import java.awt.Dimension;
import java.awt.Toolkit;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Calendar;


import javax.swing.JFrame;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

public class Main {

    /**
     * @param args
     */
	static final String VERSION = "0002";
	
    static InstructionTable INSTRUCTION_TABLE;
    static SymbolTable SYMBOL_TABLE;
    static Intermediate INTERMEDIATE_FILE;
    static Line currentline;
    static int LOCATION_COUNTER;
    static int PRE_LITERAL_POOL_LC;
    static int PREVIOUS_LC;
    static ArrayList<String> statements;
    static ErrorHandler eHandler;
    static File inputfile;
    static File outputdir;
    static String PREVIOUS_OP;
    static final int MAX_LINE_LENGTH = 115;
    static int PROGRAM_LENGTH;
    static int START_LOCATION;
    static int linecounter;
    static int START_PAGE;
    static String START_NAME;
    static Boolean valid_start = true;
    static Boolean skip = false;
    static Boolean continue_assembly = true;
    static Boolean continue_pass1 = true;
    static Boolean alternate_start = false;
    static String alternate_start_location = "0";
    static Boolean valid_end = true;


    private final static String newline = System.getProperty("line.separator");
    
    public static void main(String[] args) {
        createFrame();
        
        INSTRUCTION_TABLE = new InstructionTable();
        INSTRUCTION_TABLE.LoadInstructionTable("InstructionTable.txt");
        eHandler = new ErrorHandler();
    }

    public static void Assemble(File source){
    	SYMBOL_TABLE = new SymbolTable();
        INTERMEDIATE_FILE = new Intermediate();
    	LOCATION_COUNTER = 0;
    	PRE_LITERAL_POOL_LC = 0;
    	PREVIOUS_LC = 0;
    	statements = new ArrayList<String>();
    	PREVIOUS_OP = "";
    	START_LOCATION = 0;
    	PROGRAM_LENGTH = 0;
        linecounter = 1;
        START_PAGE = 0;
        START_NAME = "";
        valid_start = true;
        skip = false;
        continue_assembly = true;
        continue_pass1 = true;
        alternate_start = false;
        alternate_start_location = "0";
        valid_end = true;

        eHandler.clearAllErrors();
        runPassOne(source);
        if (valid_end && valid_start && continue_assembly) {
        	runPassTwo(INTERMEDIATE_FILE);
        }
      
    }	
	
	public static void outputPrint(String str){
        AssemblerFrame.getOutputArea().append(str + newline);
    }
	
	public static void errorPrint(String str){
        AssemblerFrame.getErrorArea().append(str + newline);
    }
	
	public static void lengthPrint(String str){
        AssemblerFrame.getLengthArea().append(str + newline);
    }
	
	public static void symbolPrint(String str){
        AssemblerFrame.getSymbolArea().append(str + newline);
    }
	
	public static void objectPrint(String str){
        AssemblerFrame.getObjectArea().append(str + newline);
    }
    
    private static void createFrame() {
        try {
            UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (InstantiationException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (UnsupportedLookAndFeelException e) {
            e.printStackTrace();
        }
        
        JFrame frame = new JFrame("Group 02 Assembler");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.add(new AssemblerFrame());
        frame.setMinimumSize(new Dimension(400, 700));
        frame.setVisible(true);    
        frame.setSize((int)Toolkit.getDefaultToolkit().getScreenSize().getWidth()/2, (int)Toolkit.getDefaultToolkit().getScreenSize().getHeight()/2);
        frame.setLocation((int)Toolkit.getDefaultToolkit().getScreenSize().getWidth()/4, (int)Toolkit.getDefaultToolkit().getScreenSize().getHeight()/10);
    }



    private static void runPassOne(File sourceinput) {
    	/*!
    	 *  LoadASMSource takes in a string that is the actual input files name (ex: "input.txt")
    	 *  and reads the file one line at a time. Each line is saved to a string and then we 
    	 *  remove all of the whitespace ('\t', '\n', etc.) with a single blank space. This way
    	 *  when we call split on the string we can separate each token easily without having to 
    	 *  worry about the whitespace formatting of the input. The operation then prints out the
    	 *  compressed line and calls parseLine, updateLocationCounter, and updateSymbolTable to
    	 *  parse and output everything.
    	 *  
    	 */
    	try {
    		BufferedReader in = new BufferedReader(new FileReader(sourceinput));
    		String str = null;
    		while ((str = in.readLine()) != null && continue_assembly && continue_pass1) {
    			skip = false;
    			if (str.length() > 1) {
    				
    				str = str.toUpperCase();
        			AssemblerFrame.getInputArea().append(linecounter + ": " + str + newline);
        			while(linecounter > statements.size()){
            			statements.add("");
        			}
        			statements.add(str);

    				// Check to make sure line length is bypassed
    				if (str.length() > MAX_LINE_LENGTH) {
    					// ERROR: THE LENGTH OF THE INCOMING INSTRUCTION LINE EXCEEDS THE MAXIMUM CHARACTER LIMIT
    					currentline = (new Line(linecounter, "", "SWR","000", "000", LOCATION_COUNTER, "000", "000000000000"));				
    					currentline.addError(288);
						LOCATION_COUNTER++;
    					skip = true;
    				}
  					checkGeneralLineSyntax(tokenizer(str));
    				if (str.charAt(0) != ':' && !skip) {            	
    					// Split comment from instruction 
    					if (str.contains(":")) {
    						str = str.split(":")[0];
    					}                    	
    					String[] tokens = tokenizer(str);
    					if (!skip) {
    						parseLine(tokens);
    						INTERMEDIATE_FILE.addLine(currentline);
    					}
    				}
    				linecounter++;
    			}
    		}
    		if (!valid_end && valid_start) {
    			continue_assembly = false;
    			// ERROR: PROGRAM MUST HAVE A VALID END DIRECTIVE
    			currentline.addError(011);
    		}
    		poolLiteralsAtEnd();
    		in.close();

    		// Calculate program length
    		PROGRAM_LENGTH = LOCATION_COUNTER - START_LOCATION;

    		SYMBOL_TABLE.printTable();
    		lengthPrint("Program Length Hex = " + Integer.toHexString(PROGRAM_LENGTH));
    		lengthPrint("Program Length Decimal = " + Integer.toString(PROGRAM_LENGTH));
    	} catch (IOException e) {
    	}
    } 
    
    private static void runPassTwo (Intermediate intermediate) {
    	//check if operands are symbols > check symbol table
    	Calendar cal = Calendar.getInstance();
    	
    	String prgload = (extendHex(Integer.toHexString(START_PAGE), 2) + extendHex(Integer.toHexString(START_LOCATION),4));
    	String modulelen = extendHex(Integer.toHexString(PROGRAM_LENGTH), 4);
    	String exestart = "";
    	
    	if (alternate_start) {
    		if (isNumber(alternate_start_location)) {
    			int alt_start = Integer.valueOf(alternate_start_location);
    			exestart = extendHex(Integer.toHexString(alt_start),4);
    		}
    		else if (SYMBOL_TABLE.symbolIsDefined(alternate_start_location)) {
    			int alt_start = SYMBOL_TABLE.getLocation(alternate_start_location);
    			exestart =extendHex(Integer.toHexString(alt_start),4);
    		}
    		else {
        		exestart = extendHex(Integer.toHexString(START_LOCATION),4);
    		}
    	}
    	else {
    		exestart = extendHex(Integer.toHexString(START_LOCATION),4);
        	
    	}
    	
    	String year = Integer.toString(cal.get(Calendar.YEAR));
    	String date = Integer.toString(cal.get(Calendar.DAY_OF_YEAR));

    	String hour = Integer.toString(cal.get(Calendar.HOUR_OF_DAY));
    	while(hour.length()<2){
    		hour = "0" + hour;
    	}
    	String min = Integer.toString(cal.get(Calendar.MINUTE));
    	while(min.length()<2){
    		min = "0" + min;
    	}
    	String sec = Integer.toString(cal.get(Calendar.SECOND));
    	while(sec.length()<2){
    		sec = "0" + sec;
    	}
    	
    	String datetime = year + date + "," + hour + ":" + min + ":" + sec;
    	
    	int recordcount = 0;
    	int linkcount = 0;
    	int textcount = 0;
    	int modcount = 0;
    	int adjustments = 0;
    	
    	
    	/*!
    	// USE FOR CHECKING INTERMEDIATE FILE CONT
    	for (Line line : INTERMEDIATE_FILE) {
    		objectPrint("Label: " + line.getLabel() + " Instruction: " + line.getInstruction() + " Op1: " + line.getOperands()[0] + " X Binary: " + line.getXBin() + " S Binary: " + line.getSFieldBin());
    	}
    	!*/
 		
    	outputPrint("LOC" + "\t" + "OBJ CODE" + "\t" + "FLAG" + "\t" + "STMT" + "\t" + "SOURCE STATEMENT");
    	
    	// write Header Record
    	objectPrint("H:" + START_NAME + ":" + prgload + ":" + modulelen + ":" + exestart + ":" + datetime + ":" + VERSION + ":" + "FICKELL-ASM" + ":" + START_NAME);
    	recordcount++;

    	for(Line line : INTERMEDIATE_FILE){
    		//objectPrint(line.toString());
    		
    		if(line.getSFieldBin().contains("?") ){
    			if(SYMBOL_TABLE.symbolIsDefined(line.getOperands()[1])){				
    				line.SetSField(extendBinary(Integer.toBinaryString(SYMBOL_TABLE.getLocation(line.getOperands()[1])), 12));
    			}else{
    				line = new Line(line.getStatementNumber(), "", "SWR", "000", "000", line.getLC(), "0", "0");
    			}
    		}
    		if (line.getInstruction().equals("AEXS") && !line.getOperands()[0].equals("*")) {
    			if(!SYMBOL_TABLE.symbolIsDefined(line.getOperands()[0])){				   			
    				line = new Line(line.getStatementNumber(), "", "SWR", "000", "000", line.getLC(), "0", "0");
    			}
    		}
    		if (line.getInstruction().equals("ADC")) {
    			if (!isValidHex(line.getOperands()[0])) {
    				if (!SYMBOL_TABLE.symbolIsDefined(line.getOperands()[0])) {
    					// ERROR: ADDRESS CONSTANT IS INVALID, SYMBOL IN OPERAND FIELD IS NEVER DEFINED
        				line.addError(677);
    					line = new Line(line.getStatementNumber(), "", "SWR", "000", "000", line.getLC(), "0", "0");
    				}
    				else {
    					line = new Line(line.getStatementNumber(), line.getLabel(), "ADC",  Integer.toHexString(SYMBOL_TABLE.getLocation(line.getOperands()[0])), line.getLC(), "0", "0");
    				}
    			}
    		}
    		if (line.getInstruction().equals("ADCE")) {
    			String[] operands = getExpressionOperands(line.getOperands()[0]);
    			if (areValidAdcExpressionOperands(operands)) {
    				int result = evaluateADCExpression(line);
    				if (result < -32768 || result > 32767) {
    					// ERROR: RESULT OF ADDRESS CONSTANT EXPRESSION IS OUT OF BOUNDS
        				line.addError(679);
    					line = new Line(line.getStatementNumber(), "", "SWR", "000", "000", line.getLC(), "0", "0");
    				}
    			}
    			else {
    				// ERROR: ADDRESS CONSTANT EXPRESSION CONATAINS INVALID OPERANDS 
    				line.addError(678);
    				line = new Line(line.getStatementNumber(), "", "SWR", "000", "000", line.getLC(), "0", "0");
    			}
    		}
    		
    		for(int i=0; i<line.getOperands().length; i++){
    			if(SYMBOL_TABLE.getSymbol(line.getOperands()[i]).GetUsage().equals("EXT") && !line.getInstruction().equals("EXT")){
    				if(line.getOperands()[i].charAt(0) == '-'){
        				objectPrint("M:" + (extendHex(Integer.toHexString(START_PAGE), 2) + extendHex(Integer.toHexString(line.getLC()),4)) + ":" + 1 + ":-:" + line.getOperands()[i] + ":" + START_NAME);
        				modcount++;
    				}else{
        				objectPrint("M:" + (extendHex(Integer.toHexString(START_PAGE), 2) + extendHex(Integer.toHexString(line.getLC()),4)) + ":" + 1 + ":+:" + line.getOperands()[i] + ":" + START_NAME);
        				modcount++;
    				}
    			}
    		}
    		

			String modification_record = "";
			adjustments = 0;
			if (line.getOpcount() > 0) {
				for (int i=0; i < getExpressionOperands(line.getOperands()[0]).length; i++) {
					if (!isNumber(getExpressionOperands(line.getOperands()[0])[i]) && !getExpressionOperands(line.getOperands()[0])[i].equals("*")) {
						if (SYMBOL_TABLE.symbolIsDefined(getExpressionOperands(line.getOperands()[0])[i])){
							adjustments++;
						}
					}
				}
			}
    		
    		// Print records for object file
    		if (INSTRUCTION_TABLE.isInstruction(line.getInstruction()) || line.getInstruction().equals("NUM") || line.getInstruction().equals("CHC") || line.getInstruction().equals("ADC") || line.getInstruction().equals("ADCE")) {
    			String hexcode = getInstructionLineHexCode(line);
    			objectPrint("T:" + (extendHex(Integer.toHexString(START_PAGE), 2) + extendHex(Integer.toHexString(line.getLC()),4)) + ":" + hexcode + ":" + getStatusFlag(line) + ":" + adjustments + ":" + START_NAME);
    			recordcount++;
    			textcount++;
    		}
    		
    		if (line.getInstruction().equals("ADCE")) {
    			int adjustment_count = 0;

    			if(getExpressionOperands(line.getOperands()[0]).length==2 && SYMBOL_TABLE.symbolIsDefined(getExpressionOperands(line.getOperands()[0])[0]) && SYMBOL_TABLE.symbolIsDefined(getExpressionOperands(line.getOperands()[0])[1])){
    				if(getExpressionSigns(line.getOperands()[0])[0] == "-"){
    					modification_record += '-' + ':' +  line.getOperands()[0];
    				}else{
    					modification_record += '+' + ':' +  line.getOperands()[0];
    				}
    				
    				if(getExpressionSigns(line.getOperands()[0])[0] == "-"){
    					modification_record += '-' + ':' +  line.getOperands()[1];
    				}else{
    					modification_record += '+' + ':' +  line.getOperands()[1];
    				}

    				objectPrint("M:" + (extendHex(Integer.toHexString(START_PAGE), 2) + extendHex(Integer.toHexString(line.getLC()),4)) + ":" + adjustment_count + ":" + modification_record + ":" + START_NAME);
    				recordcount++;
    				modcount++;
    			}else{
    				for (int i=0; i < getExpressionOperands(line.getOperands()[0]).length; i++) {
    					if (!isNumber(getExpressionOperands(line.getOperands()[0])[i]) && !getExpressionOperands(line.getOperands()[0])[i].equals("*")) {
    						if (SYMBOL_TABLE.symbolIsDefined(getExpressionOperands(line.getOperands()[0])[i])){
    							adjustment_count++;
    							modification_record += getExpressionSigns(line.getOperands()[0])[i] + ':' +  getExpressionOperands(line.getOperands()[0])[i] + ':';
    						}
    					}
    				}
    				
    				if((modification_record != "")&&(adjustment_count>1)){
    					recordcount++;
    					modcount++;
    					objectPrint("M:" + (extendHex(Integer.toHexString(START_PAGE), 2) + extendHex(Integer.toHexString(line.getLC()),4)) + ":" + adjustment_count + ":" + modification_record + START_NAME);
    				}
    			}
    		} else if (line.getInstruction().equals("RESET")) {
    			String label = line.getLabel();
    			objectPrint("L:" + label + ":" + (extendHex(Integer.toHexString(START_PAGE), 2) + extendHex(Integer.toHexString(line.getLC()),4)) + ":" +  START_NAME);
    			recordcount++;
    			linkcount++;
    		}
    		else if (line.getInstruction().equals("ENT")) {
    			int i = 0;
    			while (i < line.getOpcount()) {
    				objectPrint("L:" + line.getOperands()[i] + ":" + (extendHex(Integer.toHexString(START_PAGE), 2) + extendHex(Integer.toHexString(line.getLC()),4)) + ":" +  START_NAME);
        			recordcount++;
        			linkcount++;
        			i++;
        			
    			}
    		}
    		
    		String hexcode;
    		if(getInstructionLineHexCode(line) == ""){
    			hexcode = "--------";
    		}else{
    			hexcode = getInstructionLineHexCode(line);
    		}
    		
    		outputPrint(extendHex(Integer.toHexString(START_PAGE), 2) + "," + extendHex(Integer.toHexString(line.getLC()).toUpperCase(),4) + "\t" + hexcode + "\t" + getStatusFlag(line) + "\t" + line.getStatementNumber() + "\t" + statements.get(line.getStatementNumber()));
    		for(int i : line.getErrors()){
    			outputPrint("\t" + "ERROR " + i + ": " + eHandler.get(i));
    			errorPrint(line.getStatementNumber() + ": ERROR " + i + ": " + eHandler.get(i));
    		}
    	}
    	for (int i=0; i<SYMBOL_TABLE.size(); i++) {
    		if (SYMBOL_TABLE.get(i).GetLabel().charAt(0) == '=') {
    			int value = Integer.valueOf(getLiteralValue(SYMBOL_TABLE.get(i).GetLabel()));			
				outputPrint(extendHex(Integer.toHexString(START_PAGE), 2) + "," + extendHex(Integer.toHexString(PRE_LITERAL_POOL_LC).toUpperCase(),4) + "\t" + extendHex(Integer.toHexString(value),8) + "\t" + "A" + "\t" + linecounter + "\t" + SYMBOL_TABLE.get(i).GetLabel() + "\t" + "NUM\t" + Integer.toString(value));
    			objectPrint("T:" + (extendHex(Integer.toHexString(START_PAGE), 2) + extendHex(Integer.toHexString(PRE_LITERAL_POOL_LC),4)) + ":" + extendHex(Integer.toHexString(value),8) + ":" + "A" + ":" + 0 + ":" + START_NAME);
    			PRE_LITERAL_POOL_LC++;
    			linecounter++;
    			textcount++;
    			recordcount++;
    		}
    	}
    	objectPrint("E:" + extendHex(Integer.toHexString(recordcount+1), 4) + ":" + extendHex(Integer.toHexString(linkcount), 4) + ":" + extendHex(Integer.toHexString(textcount), 4) + ":" + extendHex(Integer.toHexString(modcount), 4) + ":" + START_NAME);
    }
    
    private static String getStatusFlag(Line line) {
		String flag = "";
		if (line.getInstruction().equals("CHC") || line.getInstruction().equals("NUM") ) {
			flag = "A";
		}
		else if (line.getOpcount() > 1) {
			if (isNumber(line.getOperands()[1])) {
				flag = "A";
			}
			else if (SYMBOL_TABLE.getUsage(line.getOperands()[1]).equals("EXT")) {
				flag = "M";
			}
			else if (SYMBOL_TABLE.symbolIsDefined(line.getOperands()[1])) {
				flag = "R";
			}
		}
		else if (line.getOpcount() == 1) {
			if (isNumber(line.getOperands()[0])) {
				flag = "A";
			}
			else if (SYMBOL_TABLE.getUsage(line.getOperands()[0]).equals("EXT")) {
				flag = "M";
			}
			else if (SYMBOL_TABLE.symbolIsDefined(line.getOperands()[0])) {
				flag = "R";
			}
		}
		else if (line.getOpcount() == 0) {
			flag = "A";
		}
		
		return flag;
	}

	private static String[] tokenizer(String inputline) {
    	boolean commaflag = false;
    	String[] tokens;
    	StringBuilder newstring = new StringBuilder("");
    	String outputstring = "";
		for(int i=0; i<inputline.length(); i++){
			if((inputline.charAt(i)=='\'')&&(!commaflag)){
				commaflag = true;
				newstring.append(inputline.charAt(i));
			} else if ((inputline.charAt(i)=='\'')&&(commaflag)){
				commaflag = false;
				newstring.append(inputline.charAt(i));
			} else if ((inputline.charAt(i)==' ')&&(commaflag)){
				newstring.append('_');
			}else {
				newstring.append(inputline.charAt(i));
			}
		}
		outputstring = newstring.toString().replaceAll("\\s+", Character.toString(' '));

		tokens = outputstring.split(" ");
		for(int i=3; i<tokens.length; i++){
			tokens[2] = tokens[2].concat(tokens[i]);
			tokens[i] = "";
		}
		
		if(tokens.length > 2){
			tokens[2] = tokens[2].replaceAll("_", " ");
		}

		return tokens;
	}

	private static void updateSymbolTable (String[] tokens) {
	    /*
	     *  updateSymbolTable takes the same array of tokens and adds the symbols to the table. Since tokens[1] will 
	     *  always be either "" or a symbol, the if statement checks if it is the latter and adds the symbol in there
	     *  accordingly. Based on a few example directives, use is set to either start, reset or label. If use gets
	     *  set to label, then the string representation of the label (operands/operand field) is set to string. The
	     *  symbol is then added to the SYMBOL_TABLE with the appropriate fields. 
	     */
	    String string = "";
	    if (tokens[1].equals("STT")) {
	        SYMBOL_TABLE.add(new Symbol(tokens[0], 0, "PRGM", string));
	    }
	    else if (tokens[1].equals("RESET")) {    
	        SYMBOL_TABLE.add(new Symbol(tokens[0], LOCATION_COUNTER, "RESET", string));
	    }
	    else if (tokens[1].equals("EXT")) {
	    	if (tokens.length > 2) {
	    		if (hasMultipleOperands(tokens[2])) {
	    			int i = 0;
	    			String[] operands = tokens[2].split(",");
	    			while (i < operands.length) {
	    				if (!SYMBOL_TABLE.symbolIsDefined(operands[i])) {
		            		SYMBOL_TABLE.add(new Symbol(operands[i], 0, "EXT", string));
		            	}	    				i++;
	    			}
	    		}
	    		else {
	    			SYMBOL_TABLE.add(new Symbol(tokens[2], 0, "EXT", string));
	    		}
	    	}
	    	else {
	    		// ERROR: DIRECTIVE USED CONTAINS NO REQUIRED OPERANDS
	        	currentline.addError(010);
	    	}
	    }
	    else if (tokens[1].equals("ENT")) {
	        if (hasMultipleOperands(tokens[2])) {
	            int i = 0;
	            String[] operands = tokens[2].split(",");
	            while (i < operands.length) {
	            	if (!SYMBOL_TABLE.symbolIsDefined(operands[i])) {
	            		SYMBOL_TABLE.add(new Symbol(operands[i], 0, "ENT", string));
	            	}
	                i++;
	            }
	        }
	        else {
	            SYMBOL_TABLE.add(new Symbol(tokens[2], 0, "ENT", string));
	        }
	    }
	    else if (tokens[1].equals("EQU")) {
	    	// Add equated * to table
	    	if (tokens[2].charAt(0) == '*') {
	    		SYMBOL_TABLE.add(new Symbol(tokens[0], LOCATION_COUNTER, "LABEL", Integer.toString(LOCATION_COUNTER)));
	    	}
	    	// Add equated elabel to table
	    	else if (SYMBOL_TABLE.symbolIsDefined(tokens[0])) {
	    		SYMBOL_TABLE.add(new Symbol(tokens[0], LOCATION_COUNTER, "LABEL", Integer.toString(SYMBOL_TABLE.getLocation(tokens[2]))));
	    	}
	    	// Add equated memory reference to table
	    	else if (isNumber(tokens[2])) {
	    		SYMBOL_TABLE.add(new Symbol(tokens[0], LOCATION_COUNTER, "LABEL", tokens[2]));
	    	}
	    }
	    else if (tokens[1].equals("EQUE")) {
	    	SYMBOL_TABLE.add(new Symbol(tokens[0],LOCATION_COUNTER, "LABEL", Integer.toString(evaluateEQUExpression(tokens[2]))));
	    }
	    else {
	    	if (tokens[1].equals("CLRA") || tokens[1].equals("CLRX")) {
	    		SYMBOL_TABLE.add(new Symbol(tokens[0], LOCATION_COUNTER, "LABEL", ""));
	    	}
	    	else {
	    		if (tokens.length > 2) {
	    			SYMBOL_TABLE.add(new Symbol(tokens[0], LOCATION_COUNTER, "LABEL", tokens[2]));
	    		}
	    	}
	    }
	
	}

	private static void parseLine (String[] tokens) {
        /*!
         *  parseLine takes in the array of tokens split up in loadASMSource and parses the tokens
         *  inside and outputs them based on what tokens[1] is. 
         *  
         *  **For this program, with the way we eliminated extraneous whitespace and split up the tokens,
         *    tokens[0] will always be either "" or a symbol label
         *    tokens[1] will always be the name of the instruction or directive
         *    tokens[2] (if available) will be the operands/operand field
         *    tokens[3...] will be the rest of the code (comments etc.)
         *    
         */
        // Print instruction line breakdown for an instruction
		PREVIOUS_LC = LOCATION_COUNTER;
        if (INSTRUCTION_TABLE.isInstruction(tokens[1])) {
            parseInstruction(tokens);
        }    
        // Print instruction line breakdown for a directive
        else if (INSTRUCTION_TABLE.isDirective(tokens[1])) {
            parseDirective(tokens);
        }
        else {
            //ERROR: OPERATION IS NOT A VALID INSTRUCTION OR DIRECTIVE
            currentline.addError(100);
        }
    }
    
	private static void parseInstruction (String[] tokens) {
		/*
		 *  parseInstruction takes in the same array of tokens as before and outputs them with the correct format. If 
		 *  the instruction has format 3, 5 or 6 then it prints out a slightly different output without a register
		 *  value since these instruction formats don't access registers
		 */
		if (!INSTRUCTION_TABLE.isInstruction(tokens[1])) {
            //ERROR: OPERATION IS NOT A VALID INSTRUCTION OR DIRECTIVE
			LOCATION_COUNTER++;
            currentline.addError(100);
        } 
        else {
            int format = INSTRUCTION_TABLE.findFormat(tokens[1]);
            switch (format) {

            case 1: parseAndCheckFormatOne(tokens); break;
            case 2: parseAndCheckFormatTwo(tokens); break;
            case 3: parseAndCheckFormatThree(tokens); break;
            case 5: parseAndCheckFormatFive(tokens); break;
            case 6: parseAndCheckFormatSix(tokens); break;
            case 7: parseAndCheckFormatSeven(tokens); break;
            
            }
        }
	}

	private static void parseAndCheckFormatOne (String[] tokens) {
		if (tokens.length > 2) {
			String operation = tokens[1];
			String operand = tokens[2];
			// Make sure operand field has comma separating operands
			if (operand.contains(",")) {
				// Check for commas as first or last character
				if (operand.charAt(0) != ',' && operand.charAt(operand.length()-1) != ',') {
					if (operand.split(",").length > 2) {
						//ERROR: F1 HAS EXACTLY 2 OPERANDS
						currentline = (new Line(linecounter, "", "SWR","000", "000", LOCATION_COUNTER, "000", "000000000000"));
						currentline.addError(240);
						LOCATION_COUNTER++;
					}
					else if (operand.length() > 2) {
						String op1 = operand.split(",")[0];
						String op2 = operand.split(",")[1];

						// Check for invalid star notation in register field
						if (op1.contains("*")) {
							currentline = (new Line(linecounter, "", "SWR","000", "000", LOCATION_COUNTER, "000", "000000000000"));
							currentline.addError(630);
							LOCATION_COUNTER++;
						}

						// Check for invalid literal in register field
						else if (op1.charAt(0) == '=') {
							currentline = (new Line(linecounter, "", "SWR","000", "000", LOCATION_COUNTER, "000", "000000000000"));
							currentline.addError(275);
							LOCATION_COUNTER++;
						}

						// Check for invalid expression in register field
						else if (op1.contains("+") || (op1.contains("-") && op1.charAt(0) != '-')) {
							currentline = (new Line(linecounter, "", "SWR","000", "000", LOCATION_COUNTER, "000", "000000000000"));
							currentline.addError(631);
							LOCATION_COUNTER++;
						}
						else if (tokens[2].contains("!") || tokens[2].contains("?") || tokens[2].contains("#") || 
								tokens[2].contains("_") || tokens[2].contains("/") || tokens[2].contains("\"") || 
								tokens[2].contains(".") || tokens[2].contains("@") || tokens[2].contains("$") ||
								tokens[2].contains("%") || tokens[2].contains("^") || tokens[2].contains("&") ||
								tokens[2].contains("[") || tokens[2].contains("]") || tokens[2].contains("{") ||
								tokens[2].contains("}") || tokens[2].contains("<") || tokens[2].contains(">")) {
							currentline = (new Line(linecounter, "", "SWR","000", "000", LOCATION_COUNTER, "000", "000000000000"));
							currentline.addError(621);
							LOCATION_COUNTER++;
						}
						else if (op1.charAt(0) == '-') {
							////ERROR: INVALID INDEX REGISTER USED, MUST BE IN RANGE OF 0-7
							currentline = (new Line(linecounter, "", "SWR","000", "000", LOCATION_COUNTER, "000", "000000000000"));
							currentline.addError(230);
							LOCATION_COUNTER++;
						}
						else {
							if (operation.equals("JEQ") || operation.equals("JGT") || 
									operation.equals("JLT") || operation.equals("JMP") ||
									operation.equals("JLK") || operation.equals("JDR") || operation.equals("STR")) {
								Boolean valid_index_op = true;

								// Make sure register in operand is a valid index register
								if (isNumber(op1)) {
									if (!(Double.valueOf(op1) >= 0) || !(Double.valueOf(op1) <= 7)) {
										//ERROR: INVALID INDEX REGISTER USED, MUST BE IN RANGE OF 0-7
										currentline = (new Line(linecounter, "", "SWR","000", "000", LOCATION_COUNTER, "000", "000000000000"));
										currentline.addError(229);
										LOCATION_COUNTER++;
										valid_index_op = false;
									}
								}
								else {
									if (op1.charAt(0) == '+') {
										op1 = op1.substring(1);
									}
									
									if (SYMBOL_TABLE.symbolIsDefined(op1)) {
										checkForValidLabel(op1);
										int symbol_value = Integer.valueOf(SYMBOL_TABLE.getString(op1));
										if (!(symbol_value >= 0) || !(symbol_value <= 7)){
											// ERROR: SYMBOL REPRESENTS AN INVALID REGISTER VALUE, EQUATED SYMBOL MUST REPRESENT A VALUE IN THE RANGE OF 0-7
											currentline = (new Line(linecounter, "", "SWR","000", "000", LOCATION_COUNTER, "000", "000000000000"));				
											currentline.addError(238);
											LOCATION_COUNTER++;
											valid_index_op = false;
										}             	
										else {
											op1 = SYMBOL_TABLE.getString(op1);
										}
									}
									else {
										// ERROR: SYMBOL MUST BE PREVIOUSLY EQUATED BEFORE USED AS A REGISTER OPERAND
										currentline = (new Line(linecounter, "", "SWR","000", "000", LOCATION_COUNTER, "000", "000000000000"));						
										currentline.addError(228);
										LOCATION_COUNTER++;
										valid_index_op = false;
									}
								}
								// Check Jump value for literal
								if (op2.contains("=")) {
									if (tokens[1].equals("STR")) {
										// ERROR: STR OPERATION CANNOT USE LITERALS
										currentline = (new Line(linecounter, "", "SWR","000", "000", LOCATION_COUNTER, "000", "000000000000"));						
										currentline.addError(511);
										LOCATION_COUNTER++;
										valid_index_op = false;
									}
									else {
										// ERROR: JUMP OPERATIONS CANNOT USE LITERALS
										currentline = (new Line(linecounter, "", "SWR","000", "000", LOCATION_COUNTER, "000", "000000000000"));						
										currentline.addError(509);
										LOCATION_COUNTER++;
										valid_index_op = false;
									}

								}
								// Print breakdown with valid index register and valid memory reference
								if (valid_index_op && isValidMemoryReference(op2)) {
									int register_int = Integer.valueOf(op1);

									String s_field_binary = "???";
									String s_field_hex = "???";
									String x_field_binary = "000";
									String s_symbol = "";

									// Retrieve hex and binary values based on format
									if (op2.contains("(")) {
										s_field_hex = getSHex(op2);
										if (!s_field_hex.equals("???")) {
											s_field_binary = extendBinary(Integer.toBinaryString(Integer.parseInt(s_field_hex, 16)), 12);
										}
										x_field_binary = getXBinary(op2);
										int i = 0;
										while (op2.charAt(i) != '(') {
											i++;
										}
										op2 = op2.substring(0, i);
									}
									else if (isNumber(op2)) {
										s_field_binary = extendBinary(Integer.toBinaryString(Integer.valueOf(op2)), 12);
										s_field_hex = Integer.toHexString(Integer.valueOf(op2));
										x_field_binary = "000";
									}
									else if (isLiteral(op2)) {
										s_field_hex = "???";
										x_field_binary = "000";
										String literal_value = op2.substring(1);
										if (!SYMBOL_TABLE.symbolIsDefined(op2)) {
											SYMBOL_TABLE.add(new Symbol(op2, 0, "LITERAL", literal_value));
										}
									}
									else if (op2.charAt(0) == '*' && op2.length() == 1) {
										s_field_hex = Integer.toHexString(LOCATION_COUNTER);
										s_field_binary = extendBinary(Integer.toBinaryString(LOCATION_COUNTER), 12);
										x_field_binary = "000";
									}
									else if (op2.charAt(0) == '*' && (op2.charAt(1) == '+' || op2.charAt(1) == '-')) {
										int result = 0;
										if (isNumber(op2.substring(2))) {
											if (op2.charAt(1) == '-') {
												result = LOCATION_COUNTER - Integer.valueOf(op2.substring(2));
											}
											else {
												result = LOCATION_COUNTER + Integer.valueOf(op2.substring(2));
											}
										}
										else {
											String symbol_value = SYMBOL_TABLE.getString(op2.substring(2));
											if (op2.charAt(1) == '-') {
												result = LOCATION_COUNTER - Integer.valueOf(symbol_value);
											}
											else {
												result = LOCATION_COUNTER + Integer.valueOf(symbol_value);
											}
										}
										
										
										s_field_hex = Integer.toHexString(result);
										s_field_binary = extendBinary(Integer.toBinaryString(result), 12);
										x_field_binary = "000";
									}
									else if (SYMBOL_TABLE.symbolIsDefined(op2)) {
										if (SYMBOL_TABLE.getUsage(op2).equals(("EXT"))) {
											s_field_binary = "000000000000";
											s_field_hex = "0";
											x_field_binary = "000";
										}
										else {
											int symbol_value = SYMBOL_TABLE.getLocation(op2);
											s_field_binary = extendBinary(Integer.toBinaryString(symbol_value), 12);
											s_field_hex = Integer.toHexString(symbol_value);
											x_field_binary = "000";	
										}
											
									}

									// Add valid instruction to intermediate 
									if (!tokens[0].equals("") && isValidLabel(tokens[0])) {
										currentline = (new Line(linecounter, tokens[0], tokens[1] , extendBinary(Integer.toBinaryString(register_int), 3), op2, LOCATION_COUNTER, x_field_binary, s_field_binary));	
									}
									else {
										currentline = (new Line(linecounter, "", tokens[1] , extendBinary(Integer.toBinaryString(register_int), 3), op2, LOCATION_COUNTER, x_field_binary, s_field_binary));	
									}

									if (SYMBOL_TABLE.symbolIsDefined(tokens[0])) {
										currentline.addError(675);
									}
									else if (!tokens[0].equals("") && isValidLabel(tokens[0])) {
										updateSymbolTable(tokens);
									}
									else if (!tokens[0].equals("")){
										checkForValidLabel(tokens[0]);
									}
									LOCATION_COUNTER++;
								}
								else if (valid_index_op && !isValidMemoryReference(op2)) {
									currentline = (new Line(linecounter, "", "SWR","000", "000", LOCATION_COUNTER, "000", "000000000000"));
									LOCATION_COUNTER++;
									checkMemoryReferenceSyntax(op2);
								}

							}
							// Check to see if operation is an index register operation
							else if (operation.equals("JEQX") || operation.equals("JNEQX") || 
									operation.equals("LDX") || operation.equals("ADDX")) {
								Boolean valid_index_op = true;

								// Make sure register in operand is a valid index register
								if (isNumber(op1)) {
									if (!(Double.valueOf(op1) >= 1) || !(Double.valueOf(op1) <= 7)) {
										//ERROR: INVALID INDEX REGISTER USED, MUST BE IN RANGE OF 0-7
										currentline = (new Line(linecounter, "", "SWR","000", "000", LOCATION_COUNTER, "000", "000000000000"));
										currentline.addError(230);
										LOCATION_COUNTER++;
										valid_index_op = false;
									}
								}
								else {
									if (SYMBOL_TABLE.symbolIsDefined(op1)) {
										checkForValidLabel(op1);
										int symbol_value = Integer.valueOf(SYMBOL_TABLE.getString(op1));
										if (!(symbol_value >= 1) || !(symbol_value <= 7)){
											// ERROR: SYMBOL REPRESENTS AN INVALID REGISTER VALUE, EQUATED SYMBOL MUST REPRESENT A VALUE IN THE RANGE OF 0-7
											currentline = (new Line(linecounter, "", "SWR","000", "000", LOCATION_COUNTER, "000", "000000000000"));				
											currentline.addError(237);
											LOCATION_COUNTER++;
											valid_index_op = false;
										}             	
										else {
											op1 = SYMBOL_TABLE.getString(op1);
										}
									}
									else {
										// ERROR: SYMBOL MUST BE PREVIOUSLY EQUATED BEFORE USED AS A REGISTER OPERAND
										currentline = (new Line(linecounter, "", "SWR","000", "000", LOCATION_COUNTER, "000", "000000000000"));						
										currentline.addError(228);
										LOCATION_COUNTER++;
										valid_index_op = false;
									}
								}
								// Check Jump value for literal
								if (op2.contains("=") && (operation.equals("JEQX") || operation.equals("JNEQX"))) {
									// ERROR: JUMP OPERATIONS CANNOT USE LITERALS
									currentline = (new Line(linecounter, "", "SWR","000", "000", LOCATION_COUNTER, "000", "000000000000"));						
									currentline.addError(509);
									LOCATION_COUNTER++;
									valid_index_op = false;
									
								}
								// Print breakdown with valid index register and valid memory reference
								if (valid_index_op && isValidMemoryReference(op2)) {
									int register_int = Integer.valueOf(op1);

									String s_field_binary = "???";
									String s_field_hex = "???";
									String x_field_binary = "000";

									// Retrieve hex and binary values based on format
									if (op2.contains("(")) {
										s_field_hex = getSHex(op2);
										if (!s_field_hex.equals("???")) {
											s_field_binary = extendBinary(Integer.toBinaryString(Integer.parseInt(s_field_hex, 16)), 12);
										}
									
										x_field_binary = getXBinary(op2);
										int i = 0;
										while (op2.charAt(i) != '(') {
											i++;
										}
										op2 = op2.substring(0, i);
									}
									else if (isNumber(op2)) {
										s_field_binary = extendBinary(Integer.toBinaryString(Integer.valueOf(op2)), 12);
										s_field_hex = Integer.toHexString(Integer.valueOf(op2));
										x_field_binary = "000";
									}
									else if (isLiteral(op2)) {
										s_field_hex = "???";
										x_field_binary = "000";
										String literal_value = op2.substring(1);
										if (!SYMBOL_TABLE.symbolIsDefined(op2)) {
											SYMBOL_TABLE.add(new Symbol(op2, 0, "LITERAL", literal_value));
										}
									}
									else if (op2.charAt(0) == '*' && op2.length() == 1) {
										s_field_hex = Integer.toHexString(LOCATION_COUNTER);
										s_field_binary = extendBinary(Integer.toBinaryString(LOCATION_COUNTER), 12);
										x_field_binary = "000";
									}
									else if (op2.charAt(0) == '*' && (op2.charAt(1) == '+' || op2.charAt(1) == '-')) {
										int result = 0;
										if (isNumber(op2.substring(2))) {
											if (op2.charAt(1) == '-') {
												result = LOCATION_COUNTER - Integer.valueOf(op2.substring(2));
											}
											else {
												result = LOCATION_COUNTER + Integer.valueOf(op2.substring(2));
											}
										}
										else {
											String symbol_value = SYMBOL_TABLE.getString(op2.substring(2));
											if (op2.charAt(1) == '-') {
												result = LOCATION_COUNTER - Integer.valueOf(symbol_value);
											}
											else {
												result = LOCATION_COUNTER + Integer.valueOf(symbol_value);
											}
										}
										
										s_field_hex = Integer.toHexString(result);
										s_field_binary = extendBinary(Integer.toBinaryString(result), 12);
										x_field_binary = "000";
									}
									else if (SYMBOL_TABLE.symbolIsDefined(op2)) {
										if (SYMBOL_TABLE.getUsage(op2).equals(("EXT"))) {
											s_field_binary = "000000000000";
											s_field_hex = "0";
											x_field_binary = "000";
										}
										else {
											int symbol_value = SYMBOL_TABLE.getLocation(op2);
											s_field_binary = extendBinary(Integer.toBinaryString(symbol_value), 12);
											s_field_hex = Integer.toHexString(symbol_value);
											x_field_binary = "000";	
										}
											
									}


									// Add valid instruction to intermediate 
									if (!tokens[0].equals("") && isValidLabel(tokens[0])) {
										currentline = (new Line(linecounter, tokens[0], tokens[1] , extendBinary(Integer.toBinaryString(register_int), 3), op2, LOCATION_COUNTER, x_field_binary, s_field_binary));	
									}
									else {
										currentline = (new Line(linecounter, "", tokens[1] , extendBinary(Integer.toBinaryString(register_int), 3), op2, LOCATION_COUNTER, x_field_binary, s_field_binary));	
									}
									
									if (SYMBOL_TABLE.symbolIsDefined(tokens[0])) {
										currentline.addError(675);
									}
									else if (!tokens[0].equals("") && isValidLabel(tokens[0])) {
										updateSymbolTable(tokens);
									}
									else if (!tokens[0].equals("")){
										checkForValidLabel(tokens[0]);
									}
									LOCATION_COUNTER++;
								}
								else if (valid_index_op && !isValidMemoryReference(op2)) {
									currentline = (new Line(linecounter, "", "SWR","000", "000", LOCATION_COUNTER, "000", "000000000000"));
									LOCATION_COUNTER++;
									checkMemoryReferenceSyntax(op2);
								}

							}
							else if (operation.equals("SRL") || operation.equals("SLL") || operation.equals("SRA") || operation.equals("SLA")) {
								Boolean valid_register = true, valid_shift = true;
								// Make sure register in operand is a valid arithmetic register
								if (isNumber(op1)) {
									if (!(Double.valueOf(op1) >= 0) || !(Double.valueOf(op1) <= 7)) {
										//ERROR: INVALID INDEX REGISTER USED, MUST BE IN RANGE OF 0-7
										currentline = (new Line(linecounter, "", "SWR","000", "000", LOCATION_COUNTER, "000", "000000000000"));		
										currentline.addError(229);
										LOCATION_COUNTER++;
										valid_register = false;
									}
								}
								else {
									if (SYMBOL_TABLE.symbolIsDefined(op1)) {
										checkForValidLabel(op1);
										int symbol_value = Integer.valueOf(SYMBOL_TABLE.getString(op1));
										if (!(symbol_value >= 0) || !(symbol_value <= 7)){
											// ERROR: SYMBOL REPRESENTS AN INVALID REGISTER VALUE, EQUATED SYMBOL MUST REPRESENT A VALUE IN THE RANGE OF 0-7
											currentline = (new Line(linecounter, "", "SWR","000", "000", LOCATION_COUNTER, "000", "000000000000"));						
											currentline.addError(237);
											LOCATION_COUNTER++;
											valid_register = false;
										}      
										else {
											op1 = SYMBOL_TABLE.getString(op1);
										}
									}
									else {
										// ERROR: SYMBOL MUST BE PREVIOUSLY EQUATED BEFORE USED AS A REGISTER OPERAND
										currentline = (new Line(linecounter, "", "SWR","000", "000", LOCATION_COUNTER, "000", "000000000000"));
										currentline.addError(228);
										LOCATION_COUNTER++;
										valid_register = false;
									}
								}
								// Check shift value
								String shift_bin = "000000000000";
								if (valid_register && isNumber(op2)){
									if (valid_register && (!(Double.valueOf(op2) >= 0) || !(Double.valueOf(op2) <= 32))) {
										// ERROR: SHIFT VALUE MUST BE IN RANGE OF 0-32
										currentline = (new Line(linecounter, "", "SWR","000", "000", LOCATION_COUNTER, "000", "000000000000"));
										currentline.addError(501);
										LOCATION_COUNTER++;
										valid_shift = false;
									}
									else {
										shift_bin = op2;
										shift_bin = extendBinary(Integer.toBinaryString(Integer.valueOf(shift_bin)), 12);
									}
								}
								else if (valid_register && isLiteral(op2)) {
									//ERROR: LITERAL IS NOT ALLOWED TO BE USED AS AN OPERAND FOR THIS OPERATION
									currentline = (new Line(linecounter, "", "SWR","000", "000", LOCATION_COUNTER, "000", "000000000000"));
									currentline.addError(608);
									LOCATION_COUNTER++;
									valid_shift = false;
								}
								else if (valid_register && !SYMBOL_TABLE.symbolIsDefined(op2)) {
									currentline = (new Line(linecounter, "", "SWR","000", "000", LOCATION_COUNTER, "000", "000000000000"));
									currentline.addError(607);
									LOCATION_COUNTER++;
									valid_shift = false;
								}
								else if (valid_register && op2.contains("(")) {
									// ERROR: SHIFT VALUE CANNOT USE INDEXED MEMORY REFERENCES
									currentline = (new Line(linecounter, "", "SWR","000", "000", LOCATION_COUNTER, "000", "000000000000"));
									currentline.addError(505);
									
									LOCATION_COUNTER++;
									valid_shift = false;
								}
								else if (valid_register){
									int shift_value = Integer.valueOf(SYMBOL_TABLE.getString(op2));
									if (shift_value >= 0 && shift_value <= 32) {
									shift_bin = SYMBOL_TABLE.getString(op2);
									shift_bin = extendBinary(Integer.toBinaryString(Integer.valueOf(shift_bin)), 12);
									op2 = SYMBOL_TABLE.getString(op2);
									}
									else {
										// ERROR: SHIFT VALUE MUST BE IN RANGE OF 0-32
										currentline = (new Line(linecounter, "", "SWR","000", "000", LOCATION_COUNTER, "000", "000000000000"));
										currentline.addError(508);
										LOCATION_COUNTER++;
										valid_shift = false;
									}
								}
								// Print output if valid register and valid shift value
								if (valid_register && valid_shift) {
									int register_int = Integer.valueOf(op1);

									// Add valid instruction to intermediate 
									if (!tokens[0].equals("") && isValidLabel(tokens[0])) {
										currentline = (new Line(linecounter, tokens[0], tokens[1] , extendBinary(Integer.toBinaryString(register_int), 3), op2, LOCATION_COUNTER, "000", shift_bin));	
									}
									else {
										currentline = (new Line(linecounter, "", tokens[1] , extendBinary(Integer.toBinaryString(register_int), 3), op2, LOCATION_COUNTER, "000", shift_bin));	
									}
									
									if (SYMBOL_TABLE.symbolIsDefined(tokens[0])) {
										currentline.addError(675);
									}
									else if (!tokens[0].equals("") && isValidLabel(tokens[0])) {
										updateSymbolTable(tokens);
									}
									else if (!tokens[0].equals("")){
										checkForValidLabel(tokens[0]);
									}
									LOCATION_COUNTER++;
								}

							}
							else {
								Boolean valid_register = true;
								// Make sure register in operand is a valid arithmetic register
								if (isNumber(op1)) {
									if (!(Double.valueOf(op1) >= 0) || !(Double.valueOf(op1) <= 7)) {
										//ERROR: INVALID INDEX REGISTER USED, MUST BE IN RANGE OF 0-7
										currentline = (new Line(linecounter, "", "SWR","000", "000", LOCATION_COUNTER, "000", "000000000000"));
										currentline.addError(229);
										LOCATION_COUNTER++;
										valid_register = false;
									}
								}
								else if (isLiteral(op1)) {
									// ERROR: LITERALS CANNONT BE USED IN REGISTER FIELDS
									currentline = (new Line(linecounter, "", "SWR","000", "000", LOCATION_COUNTER, "000", "000000000000"));
									currentline.addError(275);
									LOCATION_COUNTER++;
									valid_register = false;
								}
								else {

									if (SYMBOL_TABLE.symbolIsDefined(op1)) {
										checkForValidLabel(op1);
										int symbol_value = Integer.valueOf(SYMBOL_TABLE.getString(op1));
										if (!(symbol_value >= 0) || !(symbol_value <= 7)){
											// ERROR: SYMBOL REPRESENTS AN INVALID REGISTER VALUE, EQUATED SYMBOL MUST REPRESENT A VALUE IN THE RANGE OF 0-7
											currentline = (new Line(linecounter, "", "SWR","000", "000", LOCATION_COUNTER, "000", "000000000000"));
											currentline.addError(238);
											LOCATION_COUNTER++;
											valid_register = false;
										}             				
										else {
											op1 = SYMBOL_TABLE.getString(op1);
										}

									}
									else {
										// ERROR: SYMBOL USED IN OPERAND FIELD IS INVALID
										currentline = (new Line(linecounter, "", "SWR","000", "000", LOCATION_COUNTER, "000", "000000000000"));
										currentline.addError(680);
										LOCATION_COUNTER++;
										valid_register = false;
									}
								}

								if (valid_register && isValidMemoryReference(op2) && op2.length() > 3 && op2.charAt(0) == '*' && (op2.charAt(1) == '+' || op2.charAt(1) == '-')) {
									int result = 0;
									boolean check = true;
									if (isNumber(op2.substring(2))) {
										if (op2.charAt(1) == '-') {
											result = LOCATION_COUNTER - Integer.valueOf(op2.substring(2));
										}
										else {
											result = LOCATION_COUNTER + Integer.valueOf(op2.substring(2));
										}
									}
									else {

										String symbol_value = SYMBOL_TABLE.getString(op2.substring(2));
										if (op2.charAt(1) == '-') {
											result = LOCATION_COUNTER - Integer.valueOf(symbol_value);
										}
										else {
											result = LOCATION_COUNTER + Integer.valueOf(symbol_value);
										}

									}

									if (check && (!(result >= 0) || !(result <= 4095))) {
										// ERROR: MEMORY REFERENCE OUT OF BOUNDS!
										currentline = (new Line(linecounter, "", "SWR","000", "000", LOCATION_COUNTER, "000", "000000000000"));
										currentline.addError(658);
										LOCATION_COUNTER++;
										valid_register = false;
									}
								}
								//Print breakdown with valid register and memory ref
								if (valid_register && isValidMemoryReference(op2)) {
									boolean check = true;
									if (isLiteral(op2)) {
										if (getLiteralValue(op2) < -Math.pow(2,31) || getLiteralValue(op2) > Math.pow(2,31)) {
											// ERROR: LITERAL VALUE OUT OF RANGE
											currentline = (new Line(linecounter, "", "SWR","000", "000", LOCATION_COUNTER, "000", "000000000000"));
											currentline.addError(270);
											LOCATION_COUNTER++;
											check = false;
										}
										else {
											if (!SYMBOL_TABLE.symbolIsDefined(op2)) {
												SYMBOL_TABLE.add(new Symbol(op2, 0, "LITERAL", Integer.toString(getLiteralValue(op2))));
											}
										}
									}
									if (check) {
										int register_int = Integer.valueOf(op1);

										String s_field_binary = "???";
										String s_field_hex = "???";
										String x_field_binary = "000";
											
										// Retrieve hex and binary values based on format
										if (tokens[2].contains("(")) {
											s_field_hex = getSHex(op2).toUpperCase();
											if (!s_field_hex.equals("???")) {
												s_field_binary = extendBinary(Integer.toBinaryString(Integer.parseInt(s_field_hex, 16)), 12);
											}
										
											x_field_binary = getXBinary(op2);
											int i = 0;
											while (op2.charAt(i) != '(') {
												i++;
											}
											op2 = op2.substring(0, i);
										}
										else if (isNumber(op2)) {
											s_field_hex = Integer.toHexString(Integer.valueOf(op2)).toUpperCase();
											s_field_binary = extendBinary(Integer.toBinaryString(Integer.parseInt(s_field_hex, 16)), 12);
											x_field_binary = "000";
										}
										else if (isLiteral(op2)) {
											s_field_hex = "???";
											x_field_binary = "000";
											String literal_value = op2.substring(1);
											if (!SYMBOL_TABLE.symbolIsDefined(op2)) {
												SYMBOL_TABLE.add(new Symbol(op2, 0, "LITERAL", literal_value));
											}
										}
										else if (op2.charAt(0) == '*' && op2.length() == 1) {
											s_field_hex = Integer.toHexString(LOCATION_COUNTER);
											s_field_binary = extendBinary(Integer.toBinaryString(LOCATION_COUNTER), 12);
											x_field_binary = "000";
										}
										else if (op2.charAt(0) == '*' && (op2.charAt(1) == '+' || op2.charAt(1) == '-')) {
											int result = 0;
											if (isNumber(op2.substring(2))) {
												if (op2.charAt(1) == '-') {
													result = LOCATION_COUNTER - Integer.valueOf(op2.substring(2));
												}
												else {
													result = LOCATION_COUNTER + Integer.valueOf(op2.substring(2));
												}
											}
											else {
												String symbol_value = SYMBOL_TABLE.getString(op2.substring(2));
												if (op2.charAt(1) == '-') {
													result = LOCATION_COUNTER - Integer.valueOf(symbol_value);
												}
												else {
													result = LOCATION_COUNTER + Integer.valueOf(symbol_value);
												}
											}
											
											
											s_field_hex = Integer.toHexString(result);
											s_field_binary = extendBinary(Integer.toBinaryString(result), 12);
											x_field_binary = "000";
										}
										else if (SYMBOL_TABLE.symbolIsDefined(op2)) {
											if (SYMBOL_TABLE.getUsage(op2).equals(("EXT"))) {
												s_field_binary = "000000000000";
												s_field_hex = "0";
												x_field_binary = "000";
											}
											else {
												int symbol_value = SYMBOL_TABLE.getLocation(op2);
												s_field_binary = extendBinary(Integer.toBinaryString(symbol_value), 12);
												s_field_hex = Integer.toHexString(symbol_value);
												x_field_binary = "000";	
											}
												
										}

										// Add valid instruction to intermediate 
										if (!tokens[0].equals("") && isValidLabel(tokens[0])) {
											currentline = (new Line(linecounter, tokens[0], tokens[1] , extendBinary(Integer.toBinaryString(register_int), 3), op2, LOCATION_COUNTER, x_field_binary, s_field_binary));	
										}
										else {
											currentline = (new Line(linecounter, "", tokens[1] , extendBinary(Integer.toBinaryString(register_int), 3), op2, LOCATION_COUNTER, x_field_binary, s_field_binary));	
										}
										
										if (SYMBOL_TABLE.symbolIsDefined(tokens[0])) {
											currentline.addError(675);
										}
										else if (!tokens[0].equals("") && isValidLabel(tokens[0])) {
											updateSymbolTable(tokens);
										}
										else if (!tokens[0].equals("")){
											checkForValidLabel(tokens[0]);
										}
										LOCATION_COUNTER++;
									}
								}
								else if (valid_register && !isValidMemoryReference(op2)) {
									currentline = (new Line(linecounter, "", "SWR","000", "000", LOCATION_COUNTER, "000", "000000000000"));
									checkMemoryReferenceSyntax(op2);
									LOCATION_COUNTER++;
								}

							}
							
						}						
					}
					else {
						//ERROR: INSTRUCTIONS OF FORMAT 1 MUST CONTAIN TWO OPERANDS SEPARATED BY A COMMA
						currentline = (new Line(linecounter, "", "SWR","000", "000", LOCATION_COUNTER, "000", "000000000000"));
						currentline.addError(240);
						LOCATION_COUNTER++;
					}
				}
				else {
					// ERROR: COMMAS AT FIRST OR LAST CHARACTER OF OPERAND
					currentline = (new Line(linecounter, "", "SWR","000", "000", LOCATION_COUNTER, "000", "000000000000"));
					currentline.addError(624);
					LOCATION_COUNTER++;
				}
			}
			else if (tokens[1].equals("CLR")) {
				if (isNumber(tokens[2])) {
					if (Double.valueOf(tokens[2]) >= 0 && Double.valueOf(tokens[2]) <= 7) {
						// VALID
						
						// Add valid instruction to intermediate 
						if (!tokens[0].equals("") && isValidLabel(tokens[0])) {
							currentline = (new Line(linecounter, tokens[0], tokens[1] , extendBinary(Integer.toBinaryString(Integer.valueOf(tokens[2])), 3), LOCATION_COUNTER, "000", "000000000000"));	
						}
						else {
							currentline = (new Line(linecounter, "", tokens[1] , extendBinary(Integer.toBinaryString(Integer.valueOf(tokens[2])), 3), LOCATION_COUNTER, "000", "000000000000"));	
						}
						
						if (SYMBOL_TABLE.symbolIsDefined(tokens[0])) {
							currentline.addError(675);
						}
						else if (!tokens[0].equals("") && isValidLabel(tokens[0])) {
							updateSymbolTable(tokens);
						}
						else if (!tokens[0].equals("")){
							checkForValidLabel(tokens[0]);
						}
						LOCATION_COUNTER++;
					}
					else {
						// ERROR: REGISTER VALUE MUST BE IN THE RANGE OF 0-7
						currentline = (new Line(linecounter, "", "SWR","000", "000", LOCATION_COUNTER, "000", "000000000000"));
						currentline.addError(235);
						LOCATION_COUNTER++;
					}
				}
				else if (!SYMBOL_TABLE.symbolIsDefined(tokens[2])){
					// ERROR: SYMBOL MUST BE PREVIOUSLY EQUATED BEFORE USED AS A REGISTER OPERAND
					currentline = (new Line(linecounter, "", "SWR","000", "000", LOCATION_COUNTER, "000", "000000000000"));
					currentline.addError(228);
					LOCATION_COUNTER++;

				}
				else {
					String register = SYMBOL_TABLE.getString(tokens[2]);
					if (!(Double.valueOf(register) >= 0) && !(Double.valueOf(register) <= 7)) {
						// ERROR: REGISTER VALUE MUST BE IN THE RANGE OF 0-7
						currentline = (new Line(linecounter, "", "SWR","000", "000", LOCATION_COUNTER, "000", "000000000000"));
						currentline.addError(235);
						LOCATION_COUNTER++;
					}
					else {
						// VALID: CLR WITH ONE OPERAND
						
						// Add valid instruction to intermediate 
						if (!tokens[0].equals("") && isValidLabel(tokens[0])) {
							currentline = (new Line(linecounter, tokens[0], tokens[1] , extendBinary(Integer.toBinaryString(Integer.valueOf(register)), 3), LOCATION_COUNTER, "000", "000000000000"));	
						}
						else {
							currentline = (new Line(linecounter, "", tokens[1] , extendBinary(Integer.toBinaryString(Integer.valueOf(register)), 3), LOCATION_COUNTER, "000", "000000000000"));	
						}
						
						// Update label to symbol table if valid
						if (SYMBOL_TABLE.symbolIsDefined(tokens[0])) {
							currentline.addError(675);
						}
						else if (!tokens[0].equals("") && isValidLabel(tokens[0])) {
							updateSymbolTable(tokens);
						}
						else if (!tokens[0].equals("")){
							checkForValidLabel(tokens[0]);
						}
						LOCATION_COUNTER++;
					}
					// ERROR: REGISTER OPERAND MUST BE NUMERICAL 
					currentline = (new Line(linecounter, "", "SWR","000", "000", LOCATION_COUNTER, "000", "000000000000"));
					currentline.addError(235);
					LOCATION_COUNTER++;
				}
			}
			else {
				//ERROR: INSTRUCTIONS OF FORMAT 1 MUST CONTAIN TWO OPERANDS SEPARATED BY A COMMA
				currentline = (new Line(linecounter, "", "SWR","000", "000", LOCATION_COUNTER, "000", "000000000000"));
				currentline.addError(240);
				LOCATION_COUNTER++;

			}
		}
		else {
			// ERROR: INSTRUCTION 
			currentline = (new Line(linecounter, "", "SWR","000", "000", LOCATION_COUNTER, "000", "000000000000"));
			currentline.addError(500);
			LOCATION_COUNTER++;
		}
	}
	private static void parseAndCheckFormatTwo (String[] tokens) {
		Boolean valid_register = true, valid_rotate = true;
		String x_binary = "000";
		if (tokens.length > 2) {
			String operand = tokens[2];
			if (tokens[2].contains("!") || tokens[2].contains("?") || tokens[2].contains("#") || 
					tokens[2].contains("_") || tokens[2].contains("/") || tokens[2].contains("\"") || 
					tokens[2].contains(".") || tokens[2].contains("@") || tokens[2].contains("$") ||
					tokens[2].contains("%") || tokens[2].contains("^") || tokens[2].contains("&") ||
					tokens[2].contains("[") || tokens[2].contains("]") || tokens[2].contains("{") ||
					tokens[2].contains("}") || tokens[2].contains("<") || tokens[2].contains(">")) {
				currentline = (new Line(linecounter, "", "SWR","000", "000", LOCATION_COUNTER, "000", "000000000000"));
				currentline.addError(621);
				LOCATION_COUNTER++;
			}
			else if (operand.contains("=")) {
				//ERROR: FORMAT 2 CANNOT USE LITERALS
				currentline = (new Line(linecounter, "", "SWR","000", "000", LOCATION_COUNTER, "000", "000000000000"));
				currentline.addError(649);
				LOCATION_COUNTER++;
				valid_register = false;
			}
			else if (operand.contains("(") || operand.contains(")")) {
				//ERROR: FORMAT 2 CANNOT USE INDEX
				currentline = (new Line(linecounter, "", "SWR","000", "000", LOCATION_COUNTER, "000", "000000000000"));
				currentline.addError(609);
				LOCATION_COUNTER++;
				valid_register = false;
			}
			else if (operand.contains(",")) {
				if (operand.charAt(0) != ',' && operand.charAt(operand.length()-1) != ',') {
					if (operand.length() > 2) {
						String op1 = operand.split(",")[0];
						String op2 = operand.split(",")[1];
						if (operand.split(",").length > 2) {
							// ERROR: 
							currentline = (new Line(linecounter, "", "SWR","000", "000", LOCATION_COUNTER, "000", "000000000000"));
							currentline.addError(241);
							LOCATION_COUNTER++;
							valid_register = false;
						}
						// Make sure register in operand is a valid arithmetic register
						if (isNumber(op1)) {
							if (!(Double.valueOf(op1) >= 0) || !(Double.valueOf(op1) <= 7)) {
								//ERROR: INVALID ARITHMETIC REGISTER USED, MUST BE IN RANGE OF 0-7
								currentline = (new Line(linecounter, "", "SWR","000", "000", LOCATION_COUNTER, "000", "000000000000"));
								currentline.addError(229);
								LOCATION_COUNTER++;
								valid_register = false;
							}
						}
						else if (op1.charAt(0) == '-') {
							//ERROR: INVALID ARITHMETIC REGISTER USED, MUST BE IN RANGE OF 0-7
							currentline = (new Line(linecounter, "", "SWR","000", "000", LOCATION_COUNTER, "000", "000000000000"));
							currentline.addError(229);
							LOCATION_COUNTER++;
							valid_register = false;
						}
						else if (op1.contains("=")) {
							// ERROR: LITERALS CANNONT BE USED IN REGISTER FIELDS
							currentline = (new Line(linecounter, "", "SWR","000", "000", LOCATION_COUNTER, "000", "000000000000"));
							currentline.addError(275);
							LOCATION_COUNTER++;
							valid_register = false;
						}
						else if (op1.contains("*")) {
							//ERROR : STAR NOTATION NOT ALLOWED IN REGISTER FIELD
							currentline = (new Line(linecounter, "", "SWR","000", "000", LOCATION_COUNTER, "000", "000000000000"));
							currentline.addError(630);
							LOCATION_COUNTER++;
							valid_register = false;
						}
						else {
							if (SYMBOL_TABLE.symbolIsDefined(op1)) {
								checkForValidLabel(op1);
								int symbol_value = Integer.valueOf(SYMBOL_TABLE.getString(op1));
								if (!(symbol_value >= 0) || !(symbol_value <= 7)){
									// ERROR: SYMBOL REPRESENTS AN INVALID ARITHMETIC REGISTER VALUE, EQUATED SYMBOL MUST REPRESENT A VALUE IN THE RANGE OF 1-7
									currentline = (new Line(linecounter, "", "SWR","000", "000", LOCATION_COUNTER, "000", "000000000000"));
									currentline.addError(238);
									LOCATION_COUNTER++;
									valid_register = false;
								}
								else {
									op1 = SYMBOL_TABLE.getString(op1);
								}
							}
							else if (op1.contains("+")) {
								// ERROR: EXPRESSIONS ARE NOT ALLOWED IN REGISTER FIELDS
								currentline = (new Line(linecounter, "", "SWR","000", "000", LOCATION_COUNTER, "000", "000000000000"));
								currentline.addError(631);
								LOCATION_COUNTER++;
								valid_register = false;
							}
							else {
								// ERROR: SYMBOL MUST BE PREVIOUSLY EQUATED BEFORE USED AS A REGISTER OPERAND
								currentline = (new Line(linecounter, "", "SWR","000", "000", LOCATION_COUNTER, "000", "000000000000"));
								currentline.addError(228);
								LOCATION_COUNTER++;
								valid_register = false;
							}

						}
						if (valid_register && tokens[1].equals("SQV")) {
							if (op2.charAt(0) == '-') {
								// ERROR: INVALID SQUARE VALUE 
								currentline = (new Line(linecounter, "", "SWR","000", "000", LOCATION_COUNTER, "000", "000000000000"));
								currentline.addError(510);
								LOCATION_COUNTER++;
							}
							else if (isNumber(op2)) {
								if (!(Double.valueOf(op2) >= 0) || !(Double.valueOf(op2) <= 4095)) {
									// ERROR: INVALID SQUARE VALUE 
									currentline = (new Line(linecounter, "", "SWR","000", "000", LOCATION_COUNTER, "000", "000000000000"));
									currentline.addError(510);
									LOCATION_COUNTER++;
								}
								else {
									int register_int = Integer.valueOf(op1);

									// Add valid instruction to intermediate 
									if (!tokens[0].equals("") && isValidLabel(tokens[0])) {
										currentline = (new Line(linecounter, tokens[0], tokens[1] , extendBinary(Integer.toBinaryString(register_int), 3), op2, LOCATION_COUNTER, x_binary, extendBinary(Integer.toBinaryString(Integer.valueOf(op2)), 12)));	
									}
									else {
										currentline = (new Line(linecounter, "", tokens[1] , extendBinary(Integer.toBinaryString(register_int), 3), op2, LOCATION_COUNTER, x_binary, extendBinary(Integer.toBinaryString(Integer.valueOf(op2)), 12)));	
									}

									if (SYMBOL_TABLE.symbolIsDefined(tokens[0])) {
										currentline.addError(675);
									}
									else if (!tokens[0].equals("") && isValidLabel(tokens[0])) {
										updateSymbolTable(tokens);
									}
									else if (!tokens[0].equals("")){
										checkForValidLabel(tokens[0]);
									}
									LOCATION_COUNTER++;
								}
							}
							else if (op2.charAt(0) == '*') {
								if (op2.substring(0).length() > 1) {
									if (op2.substring(0).length() > 2) {
										if (op2.substring(1).contains("*")) {
											// ERROR: ONLY ONE STAR IN STAR NOTATION
											currentline = (new Line(linecounter, "", "SWR","000", "000", LOCATION_COUNTER, "000", "000000000000"));
											currentline.addError(289);
											LOCATION_COUNTER++;
										}
										else if (op2.contains("++") || op2.contains("--") || op2.contains("+-") || op2.contains("-+")) {
											// ERROR: BACK TO BACK OPERATORS NOT ALLOWED
											currentline = (new Line(linecounter, "", "SWR","000", "000", LOCATION_COUNTER, "000", "000000000000"));
											currentline.addError(311);
											LOCATION_COUNTER++;
										}
										else if (op2.substring(1).charAt(0) != '+' && op2.substring(1).charAt(0) != '-') {
											// ERROR: OPERATOR MUST FOLLOW *
											currentline = (new Line(linecounter, "", "SWR","000", "000", LOCATION_COUNTER, "000", "000000000000"));
											currentline.addError(308);
											LOCATION_COUNTER++;
										}
										else if (op2.substring(1).charAt(0) == '+' || op2.substring(1).charAt(0) == '-') {
											Boolean negative = false;
											if (op2.substring(1).charAt(0) == '-') {
												negative = true;
											}
											String value = op2.substring(2);
											if (isNumber(value)) {
												int result = 0;
												if (negative) {
													result = LOCATION_COUNTER - Integer.valueOf(value);
												}
												else {
													result = LOCATION_COUNTER + Integer.valueOf(value);
												}
												// Check result
												if (result >= 0 && result <= 4095) {
													int register_int = Integer.valueOf(op1);

													// Add valid instruction to intermediate 
													if (!tokens[0].equals("") && isValidLabel(tokens[0])) {
														currentline = (new Line(linecounter, tokens[0], tokens[1] , extendBinary(Integer.toBinaryString(register_int), 3), op2, LOCATION_COUNTER, x_binary, extendBinary(Integer.toBinaryString(result), 12)));	
													}
													else {
														currentline = (new Line(linecounter, "", tokens[1] , extendBinary(Integer.toBinaryString(register_int), 3), op2, LOCATION_COUNTER, x_binary, extendBinary(Integer.toBinaryString(result), 12)));	
													}
													
													if (SYMBOL_TABLE.symbolIsDefined(tokens[0])) {
														currentline.addError(675);
													}
													else if (!tokens[0].equals("") && isValidLabel(tokens[0])) {
														updateSymbolTable(tokens);
													}
													else if (!tokens[0].equals("")){
														checkForValidLabel(tokens[0]);
													}
													LOCATION_COUNTER++;
												}
												else {
													// ERROR: OUT OF BOUNDS
													currentline = (new Line(linecounter, "", "SWR","000", "000", LOCATION_COUNTER, "000", "000000000000"));
													currentline.addError(510);
													LOCATION_COUNTER++;
												}
											}
											else if (SYMBOL_TABLE.symbolIsDefined(value)) {
												int result = 0;
												if (negative) {
													result = LOCATION_COUNTER - Integer.valueOf(SYMBOL_TABLE.getString(value));
												}
												else {
													result = LOCATION_COUNTER + Integer.valueOf(SYMBOL_TABLE.getString(value));
												}
												// Check result
												if (result >= 0 && result <= 4095) {
													int register_int = Integer.valueOf(op1);

													// Add valid instruction to intermediate 
													if (!tokens[0].equals("") && isValidLabel(tokens[0])) {
														currentline = (new Line(linecounter, tokens[0], tokens[1] , extendBinary(Integer.toBinaryString(register_int), 3), op2, LOCATION_COUNTER, x_binary, extendBinary(Integer.toBinaryString(result), 12)));	
													}
													else {
														currentline = (new Line(linecounter, "", tokens[1] , extendBinary(Integer.toBinaryString(register_int), 3), op2, LOCATION_COUNTER, x_binary, extendBinary(Integer.toBinaryString(result), 12)));	
													}

													if (SYMBOL_TABLE.symbolIsDefined(tokens[0])) {
														currentline.addError(675);
													}
													else if (!tokens[0].equals("") && isValidLabel(tokens[0])) {
														updateSymbolTable(tokens);
													}
													else if (!tokens[0].equals("")){
														checkForValidLabel(tokens[0]);
													}
													LOCATION_COUNTER++;
												}
												else {
													// ERROR: OUT OF BOUNDS
													currentline = (new Line(linecounter, "", "SWR","000", "000", LOCATION_COUNTER, "000", "000000000000"));
													currentline.addError(510);
													LOCATION_COUNTER++;
												}
											}
											else {
												// ERROR: FORMAT 2 INSTRUCTIONS USING STAR AND AN OPERATOR MUST BE FOLLOWD BY A CONSTANT 
												currentline = (new Line(linecounter, "", "SWR","000", "000", LOCATION_COUNTER, "000", "000000000000"));
												currentline.addError(648);
												LOCATION_COUNTER++;
											}
										}
									}
									else {
										// ERROR: MUST HAVE OPERATOR AND VALUE AFTER *
										currentline = (new Line(linecounter, "", "SWR","000", "000", LOCATION_COUNTER, "000", "000000000000"));
										currentline.addError(308);
										LOCATION_COUNTER++;
										
									}
								}
								else {
									// VALID ONE STAR!
									int register_int = Integer.valueOf(op1);

									// Add valid instruction to intermediate 
									if (!tokens[0].equals("") && isValidLabel(tokens[0])) {
										currentline = (new Line(linecounter, tokens[0], tokens[1] , extendBinary(Integer.toBinaryString(register_int), 3), op2, LOCATION_COUNTER, x_binary, extendBinary(Integer.toBinaryString(LOCATION_COUNTER), 12)));	
									}
									else {
										currentline = (new Line(linecounter, "", tokens[1] , extendBinary(Integer.toBinaryString(register_int), 3), op2, LOCATION_COUNTER, x_binary, extendBinary(Integer.toBinaryString(LOCATION_COUNTER), 12)));	
									}
									
									if (SYMBOL_TABLE.symbolIsDefined(tokens[0])) {
										currentline.addError(675);
									}
									else if (!tokens[0].equals("") && isValidLabel(tokens[0])) {
										updateSymbolTable(tokens);
									}
									else if (!tokens[0].equals("")){
										checkForValidLabel(tokens[0]);
									}
									LOCATION_COUNTER++;
								}
							}
							else if (op2.contains("*")) {
								// ERROR: STAR HAS TO BE AT FRONT
								currentline = (new Line(linecounter, "", "SWR","000", "000", LOCATION_COUNTER, "000", "000000000000"));
								currentline.addError(290);
								LOCATION_COUNTER++;
							}
							else if (SYMBOL_TABLE.symbolIsDefined(op2)) {
								int value = Integer.valueOf(SYMBOL_TABLE.getLocation(op2));
								int register_int = Integer.valueOf(op1);


								// Add valid instruction to intermediate 
								if (!tokens[0].equals("") && isValidLabel(tokens[0])) {
									currentline = (new Line(linecounter, tokens[0], tokens[1] , extendBinary(Integer.toBinaryString(register_int), 3), op2, LOCATION_COUNTER, x_binary, extendBinary(Integer.toBinaryString(value), 12)));	
								}
								else {
									currentline = (new Line(linecounter, "", tokens[1] , extendBinary(Integer.toBinaryString(register_int), 3), op2, LOCATION_COUNTER, x_binary, extendBinary(Integer.toBinaryString(value), 12)));	
								}
								
								if (SYMBOL_TABLE.symbolIsDefined(tokens[0])) {
									currentline.addError(675);
								}
								else if (!tokens[0].equals("") && isValidLabel(tokens[0])) {
									updateSymbolTable(tokens);
								}
								else if (!tokens[0].equals("")){
									checkForValidLabel(tokens[0]);
								}
								LOCATION_COUNTER++;
								
							}
							else {
								// SYMBOL NOT DEFINED YET
								int register_int = Integer.valueOf(op1);

								currentline.addError(310);
								// Add valid instruction to intermediate 
								if (!tokens[0].equals("") && isValidLabel(tokens[0])) {
									currentline = (new Line(linecounter, tokens[0], tokens[1] , extendBinary(Integer.toBinaryString(register_int), 3), op2, LOCATION_COUNTER, x_binary, "???"));	
								}
								else {
									currentline = (new Line(linecounter, "", tokens[1] , extendBinary(Integer.toBinaryString(register_int), 3), op2, LOCATION_COUNTER, x_binary, "???"));	
								}

								if (!tokens[0].equals("") && isValidLabel(tokens[0])) {
									updateSymbolTable(tokens);
								}
								else if (!tokens[0].equals("")){
									checkForValidLabel(tokens[0]);
								}
								LOCATION_COUNTER++;
							}
							/*!
							else if (isLiteral(op2)) {
								if (isValidLiteral(op2)) {
									if (getLiteralValue(op2) < 0 || getLiteralValue(op2) > 4095) {
										// ERROR: LITERAL VALUE OUT OF RANGE
										outputPrint("Label=" + tokens[0] + ", Instruction=" + tokens[1] + " Reg=" + op1 + " Memory_reference:pg:" + START_PAGE + ", LC=" + Integer.toHexString(LOCATION_COUNTER).toUpperCase());
										outputPrint("Binary_opcode=" + INSTRUCTION_TABLE.findOpcode(tokens[1]) + " Binary_R=???" + " Binary_X=???" + " Binary_pg=" + Integer.toBinaryString(START_PAGE) + " Hex s-field=???");
										eHandler.addError(270);
										currentline = (new Line(STATEMENT_LOCATION, "", "SWR","000", "000", LOCATION_COUNTER, "000", "000000000000"));
										LOCATION_COUNTER++;
									}
									else {
										if (!SYMBOL_TABLE.symbolIsDefined(op2)) {
											SYMBOL_TABLE.add(new Symbol(op2, 0, "LITERAL", Integer.toString(getLiteralValue(op2))));
										}
										int register_int = Integer.valueOf(op1);
										outputPrint("Label=" + tokens[0] + ", Instruction=" + tokens[1] + " Reg=" + op1 + " Memory_reference:pg:" + START_PAGE + ", LC=" + Integer.toHexString(LOCATION_COUNTER).toUpperCase());
										outputPrint("Binary_opcode=" + INSTRUCTION_TABLE.findOpcode(tokens[1]) + " Binary_R=" + Integer.toBinaryString(register_int) + " Binary_X=000" + " Binary_pg=" + Integer.toBinaryString(START_PAGE) + " Hex s-field=???");

										// Add valid instruction to intermediate 
										if (!tokens[0].equals("") && isValidLabel(tokens[0])) {
											currentline = (new Line(STATEMENT_LOCATION, tokens[0], tokens[1] , extendBinary(Integer.toBinaryString(register_int), 3), op2, LOCATION_COUNTER, "000", "000000000000"));	
										}
										else {
											currentline = (new Line(STATEMENT_LOCATION, "", tokens[1] , extendBinary(Integer.toBinaryString(register_int), 3), op2, LOCATION_COUNTER,  "000", "000000000000"));	
										}

										if (!tokens[0].equals("") && isValidLabel(tokens[0])) {
											updateSymbolTable(tokens);
										}
										else if (!tokens[0].equals("")){
											checkForValidLabel(tokens[0]);
										}
										LOCATION_COUNTER++;
									}
									
								}
								else {
									// ERROR: INVALID LITERAL SYNTAX
									outputPrint("Label=" + tokens[0] + ", Instruction=" + tokens[1] + " Reg=" + op1 + " Memory_reference:pg:" + START_PAGE + ", LC=" + Integer.toHexString(LOCATION_COUNTER).toUpperCase());
									outputPrint("Binary_opcode=" + INSTRUCTION_TABLE.findOpcode(tokens[1]) + " Binary_R=???" + " Binary_X=???" + " Binary_pg=" + Integer.toBinaryString(START_PAGE) + " Hex s-field=???");
									eHandler.addError(272);
									currentline = (new Line(STATEMENT_LOCATION, "", "SWR","000", "000", LOCATION_COUNTER, "000", "000000000000"));
									LOCATION_COUNTER++;
								}
							}
						
							else if (!SYMBOL_TABLE.symbolIsDefined(op2)) {
								// SYMBOL MUST BE PREVIOUSLY EQUATED BEFORE BEING USED AS AN OPERAND
								outputPrint("Label=" + tokens[0] + ", Instruction=" + tokens[1] + " Reg=" + op1 + " Memory_reference:pg:" + START_PAGE + ", LC=" + Integer.toHexString(LOCATION_COUNTER).toUpperCase());
								outputPrint("Binary_opcode=" + INSTRUCTION_TABLE.findOpcode(tokens[1]) + " Binary_R=???" + " Binary_X=???" + " Binary_pg=" + Integer.toBinaryString(START_PAGE) + " Hex s-field=???");
								currentline = (new Line(STATEMENT_LOCATION, "", "SWR","000", "000", LOCATION_COUNTER, "000", "000000000000"));
								LOCATION_COUNTER++;
								eHandler.addError(607);
							}
							else if (SYMBOL_TABLE.symbolIsDefined(op2)) {
								String square_value = SYMBOL_TABLE.getString(op2);
								int register_int = Integer.valueOf(op1);
								if (Double.valueOf(square_value) >= 0 && Double.valueOf(square_value) <= 4095) {
									outputPrint("Label=" + tokens[0] + ", Instruction=" + tokens[1] + " Reg=" + op1 + " Memory_reference:pg:" + START_PAGE + ", LC=" + Integer.toHexString(LOCATION_COUNTER).toUpperCase());
									outputPrint("Binary_opcode=" + INSTRUCTION_TABLE.findOpcode(tokens[1]) + " Binary_R=" + Integer.toBinaryString(register_int) + " Binary_X=000" + " Binary_pg=" + Integer.toBinaryString(START_PAGE) + " Hex s-field=" + Integer.toHexString(Integer.valueOf(square_value)).toUpperCase());

									// Add valid instruction to intermediate 
									if (!tokens[0].equals("") && isValidLabel(tokens[0])) {
										currentline = (new Line(STATEMENT_LOCATION, tokens[0], tokens[1] , extendBinary(Integer.toBinaryString(register_int), 3), op2, LOCATION_COUNTER, x_binary,extendBinary(Integer.toBinaryString(Integer.valueOf(square_value)), 12)));	
									}
									else {
										currentline = (new Line(STATEMENT_LOCATION, "", tokens[1] , extendBinary(Integer.toBinaryString(register_int), 3), op2, LOCATION_COUNTER, x_binary,extendBinary(Integer.toBinaryString(Integer.valueOf(square_value)), 12)));	
									}

									if (!tokens[0].equals("") && isValidLabel(tokens[0])) {
										updateSymbolTable(tokens);
									}
									else if (!tokens[0].equals("")){
										checkForValidLabel(tokens[0]);
									}
									LOCATION_COUNTER++;
								}
							}	
							!*/			
							
						}
						else if (valid_register && (tokens[1].equals("ROL") || tokens[1].equals("ROR"))) {
							if (isNumber(op2)) {
								if (!(Double.valueOf(op2) >= 0) || !(Double.valueOf(op2) <= 32)) {
									// ERROR: INVALID ROTATE VALUE
									currentline = (new Line(linecounter, "", "SWR","000", "000", LOCATION_COUNTER, "000", "000000000000"));
									currentline.addError(502);
									LOCATION_COUNTER++;
								}
								else {
									int register_int = Integer.valueOf(op1);

									// Add valid instruction to intermediate 
									if (!tokens[0].equals("") && isValidLabel(tokens[0])) {
										currentline = (new Line(linecounter, tokens[0], tokens[1] , extendBinary(Integer.toBinaryString(register_int), 3), op2, LOCATION_COUNTER, x_binary, extendBinary(Integer.toBinaryString(Integer.valueOf(op2)), 12)));	
									}
									else {
										currentline = (new Line(linecounter, "", tokens[1] , extendBinary(Integer.toBinaryString(register_int), 3), op2, LOCATION_COUNTER, x_binary, extendBinary(Integer.toBinaryString(Integer.valueOf(op2)), 12)));	
									}

									if (SYMBOL_TABLE.symbolIsDefined(tokens[0])) {
										currentline.addError(675);
									}
									else if (!tokens[0].equals("") && isValidLabel(tokens[0])) {
										updateSymbolTable(tokens);
									}
									else if (!tokens[0].equals("")){
										checkForValidLabel(tokens[0]);
									}
									LOCATION_COUNTER++;
								}
							}
							else if (!SYMBOL_TABLE.symbolIsDefined(op2)) {
								// SYMBOL MUST BE PREVIOUSLY EQUATED BEFORE BEING USED AS AN OPERAND
								currentline = (new Line(linecounter, "", "SWR","000", "000", LOCATION_COUNTER, "000", "000000000000"));
								currentline.addError(607);
								LOCATION_COUNTER++;
							}
							else if (SYMBOL_TABLE.symbolIsDefined(op2)) {
								String rotate_value = SYMBOL_TABLE.getString(op2);
								int register_int = Integer.valueOf(op1);
								if (Double.valueOf(rotate_value) >= 0 && Double.valueOf(rotate_value) <= 32) {

									// Add valid instruction to intermediate 
									if (!tokens[0].equals("") && isValidLabel(tokens[0])) {
										currentline = (new Line(linecounter, tokens[0], tokens[1] , extendBinary(Integer.toBinaryString(register_int), 3), op2, LOCATION_COUNTER, x_binary,extendBinary(Integer.toBinaryString(Integer.valueOf(rotate_value)), 12)));	
									}
									else {
										currentline = (new Line(linecounter, "", tokens[1] , extendBinary(Integer.toBinaryString(register_int), 3), op2, LOCATION_COUNTER, x_binary,extendBinary(Integer.toBinaryString(Integer.valueOf(rotate_value)), 12)));	
									}

									if (SYMBOL_TABLE.symbolIsDefined(tokens[0])) {
										currentline.addError(675);
									}
									else if (!tokens[0].equals("") && isValidLabel(tokens[0])) {
										updateSymbolTable(tokens);
									}
									else if (!tokens[0].equals("")){
										checkForValidLabel(tokens[0]);
									}
									LOCATION_COUNTER++;
								}
								else {
									// ERROR: INVALID ROTATE VALUE
									currentline = (new Line(linecounter, "", "SWR","000", "000", LOCATION_COUNTER, "000", "000000000000"));
									currentline.addError(502);
									LOCATION_COUNTER++;
								}
							}
							else {
								// ERROR: INVALID ROTATE VALUE FORMAT
								currentline = (new Line(linecounter, "", "SWR","000", "000", LOCATION_COUNTER, "000", "000000000000"));
								currentline.addError(504);
								LOCATION_COUNTER++;
							}
						}
						else if (op2.charAt(0) == '*') {
							if (op2.substring(0).length() > 1) {
								if (op2.substring(0).length() > 2) {
									if (op2.substring(1).contains("*")) {
										// ERROR: ONLY ONE STAR IN STAR NOTATION
										currentline = (new Line(linecounter, "", "SWR","000", "000", LOCATION_COUNTER, "000", "000000000000"));
										currentline.addError(289);
										LOCATION_COUNTER++;
									}
									else if (op2.contains("++") || op2.contains("--") || op2.contains("+-") || op2.contains("-+")) {
										// ERROR: BACK TO BACK OPERATORS NOT ALLOWED
										currentline = (new Line(linecounter, "", "SWR","000", "000", LOCATION_COUNTER, "000", "000000000000"));
										currentline.addError(311);
										LOCATION_COUNTER++;
									}
									else if (op2.substring(1).charAt(0) != '+' && op2.substring(1).charAt(0) != '-') {
										// ERROR: OPERATOR MUST FOLLOW *
										currentline = (new Line(linecounter, "", "SWR","000", "000", LOCATION_COUNTER, "000", "000000000000"));
										currentline.addError(308);
										LOCATION_COUNTER++;
									}
									else if (op2.substring(1).charAt(0) == '+' || op2.substring(1).charAt(0) == '-') {
										Boolean negative = false;
										if (op2.substring(1).charAt(0) == '-') {
											negative = true;
										}
										String value = op2.substring(2);
										if (isNumber(value)) {
											int result = 0;
											if (negative) {
												result = LOCATION_COUNTER - Integer.valueOf(value);
											}
											else {
												result = LOCATION_COUNTER + Integer.valueOf(value);
											}
											// Check result
											if (result >= 0 && result <= 4095) {
												int register_int = Integer.valueOf(op1);

												// Add valid instruction to intermediate 
												if (!tokens[0].equals("") && isValidLabel(tokens[0])) {
													currentline = (new Line(linecounter, tokens[0], tokens[1] , extendBinary(Integer.toBinaryString(register_int), 3), op2, LOCATION_COUNTER, x_binary, extendBinary(Integer.toBinaryString(result), 12)));	
												}
												else {
													currentline = (new Line(linecounter, "", tokens[1] , extendBinary(Integer.toBinaryString(register_int), 3), op2, LOCATION_COUNTER, x_binary, extendBinary(Integer.toBinaryString(result), 12)));	
												}

												if (SYMBOL_TABLE.symbolIsDefined(tokens[0])) {
													currentline.addError(675);
												}
												else if (!tokens[0].equals("") && isValidLabel(tokens[0])) {
													updateSymbolTable(tokens);
												}
												else if (!tokens[0].equals("")){
													checkForValidLabel(tokens[0]);
												}
												LOCATION_COUNTER++;
											}
											else {
												// ERROR: OUT OF BOUNDS
												currentline = (new Line(linecounter, "", "SWR","000", "000", LOCATION_COUNTER, "000", "000000000000"));
												currentline.addError(510);
												LOCATION_COUNTER++;
											}
										}
										else {
											// ERROR: NUMERICAL CONSTANT MUST FOLLOW STAR NOTATION
											currentline = (new Line(linecounter, "", "SWR","000", "000", LOCATION_COUNTER, "000", "000000000000"));
											currentline.addError(648);
											LOCATION_COUNTER++;
										}
									}
								}
								else {
									// ERROR: MUST HAVE OPERATOR AND VALUE AFTER *
									currentline = (new Line(linecounter, "", "SWR","000", "000", LOCATION_COUNTER, "000", "000000000000"));
									currentline.addError(308);
									LOCATION_COUNTER++;
									
								}
							}
							else {
								// VALID ONE STAR!
								int register_int = Integer.valueOf(op1);

								// Add valid instruction to intermediate 
								if (!tokens[0].equals("") && isValidLabel(tokens[0])) {
									currentline = (new Line(linecounter, tokens[0], tokens[1] , extendBinary(Integer.toBinaryString(register_int), 3), op2, LOCATION_COUNTER, x_binary, extendBinary(Integer.toBinaryString(LOCATION_COUNTER), 12)));	
								}
								else {
									currentline = (new Line(linecounter, "", tokens[1] , extendBinary(Integer.toBinaryString(register_int), 3), op2, LOCATION_COUNTER, x_binary, extendBinary(Integer.toBinaryString(LOCATION_COUNTER), 12)));	
								}

								if (SYMBOL_TABLE.symbolIsDefined(tokens[0])) {
									currentline.addError(675);
								}
								else if (!tokens[0].equals("") && isValidLabel(tokens[0])) {
									updateSymbolTable(tokens);
								}
								else if (!tokens[0].equals("")){
									checkForValidLabel(tokens[0]);
								}
								LOCATION_COUNTER++;
							}
						}
						else if (op2.contains("*")) {
							// ERROR: STAR HAS TO BE AT FRONT
							currentline = (new Line(linecounter, "", "SWR","000", "000", LOCATION_COUNTER, "000", "000000000000"));
							currentline.addError(290);
							LOCATION_COUNTER++;
						}
						
						/*!
						if (isLiteral(op2)) {
							if (getLiteralValue(op2) < 0 || getLiteralValue(op2) > 4095) {
								// ERROR: LITERAL VALUE OUT OF RANGE
								outputPrint("Label=" + tokens[0] + ", Instruction=" + tokens[1] + " Reg=" + op1 + " Memory_reference:pg:" + START_PAGE + ", LC=" + Integer.toHexString(LOCATION_COUNTER).toUpperCase());
								outputPrint("Binary_opcode=" + INSTRUCTION_TABLE.findOpcode(tokens[1]) + " Binary_R=???" + " Binary_X=???" + " Binary_pg=" + Integer.toBinaryString(START_PAGE) + " Hex s-field=???");
								eHandler.addError(270);
								currentline = (new Line(STATEMENT_LOCATION, "", "SWR","000", "000", LOCATION_COUNTER, "000", "000000000000"));
								LOCATION_COUNTER++;
								valid_register = false;
							}
							else {
								if (!SYMBOL_TABLE.symbolIsDefined(op2)) {
									SYMBOL_TABLE.add(new Symbol(op2, 0, "LITERAL", Integer.toString(getLiteralValue(op2))));
								}
							}
						}
						!*/
						else if(valid_register) {
							
							//Print breakdown with valid register and memory ref
							if (valid_register && isValidMemoryReference(op2)) {
								int register_int = Integer.valueOf(op1);

								String s_field_binary = "???";
								String s_field_hex = "???";
								String x_field_binary = "000";

								// Retrieve hex and binary values based on format
								if (isNumber(op2)) {
									s_field_hex = Integer.toHexString(Integer.valueOf(op2)).toUpperCase();
									s_field_binary = extendBinary(Integer.toBinaryString(Integer.valueOf(op2)), 12);
									x_field_binary = "000";

								}
								else if (op2.contains("(")) {
									// ERROR: MEMORY REFERENCE IN FORMAT 2 CANNOT HAVE AN INDEX VALUE
									currentline = (new Line(linecounter, "", "SWR","000", "000", LOCATION_COUNTER, "000", "000000000000"));
									currentline.addError(609);
									LOCATION_COUNTER++;
								}
								else if (isLiteral(op2)) {
									s_field_hex = "???";
									x_field_binary = "000";
									String literal_value = op2.substring(1);
									if (!SYMBOL_TABLE.symbolIsDefined(op2)) {
										SYMBOL_TABLE.add(new Symbol(op2, 0, "LITERAL", literal_value));
									}

								}
								else if (op2.charAt(0) == '*' && op2.length() == 1) {
									s_field_hex = Integer.toHexString(LOCATION_COUNTER);
									s_field_binary = extendBinary(Integer.toBinaryString(LOCATION_COUNTER), 12);
									x_field_binary = "000";
								}
								else if (op2.charAt(0) == '*' && (op2.charAt(1) == '+' || op2.charAt(1) == '-')) {
									int result = 0;
									if (isNumber(op2.substring(2))) {
										if (op2.charAt(1) == '-') {
											result = LOCATION_COUNTER - Integer.valueOf(op2.substring(2));
										}
										else {
											result = LOCATION_COUNTER + Integer.valueOf(op2.substring(2));
										}
									}
									else {
										String symbol_value = SYMBOL_TABLE.getString(op2.substring(2));
										if (op2.charAt(1) == '-') {
											result = LOCATION_COUNTER - Integer.valueOf(symbol_value);
										}
										else {
											result = LOCATION_COUNTER + Integer.valueOf(symbol_value);
										}
									}
									
									
									s_field_hex = Integer.toHexString(result);
									s_field_binary = extendBinary(Integer.toBinaryString(result), 12);
									x_field_binary = "000";
								}
								else if (SYMBOL_TABLE.symbolIsDefined(op2)) {
									int symbol_value = SYMBOL_TABLE.getLocation(op2);
									s_field_hex = Integer.toHexString(symbol_value).toUpperCase();
									s_field_binary = extendBinary(Integer.toBinaryString(Integer.valueOf(SYMBOL_TABLE.getLocation(op2))), 12);
									x_field_binary = "000";	

								}

								// Add valid instruction to intermediate 
								if (!tokens[0].equals("") && isValidLabel(tokens[0])) {
									currentline = (new Line(linecounter, tokens[0], tokens[1] , extendBinary(Integer.toBinaryString(register_int), 3), op2, LOCATION_COUNTER, x_field_binary, s_field_hex));	
								}
								else {
									currentline = (new Line(linecounter, "", tokens[1] , extendBinary(Integer.toBinaryString(register_int), 3), op2, LOCATION_COUNTER, x_field_binary, s_field_hex));	
								}

								if (SYMBOL_TABLE.symbolIsDefined(tokens[0])) {
									currentline.addError(675);
								}
								else if (!tokens[0].equals("") && isValidLabel(tokens[0])) {
									updateSymbolTable(tokens);
								}
								else if (!tokens[0].equals("")){
									checkForValidLabel(tokens[0]);
								}
								LOCATION_COUNTER++;
							}
							else if (valid_register && !isValidMemoryReference(op2)) {
								checkMemoryReferenceSyntax(op2);
								currentline = (new Line(linecounter, "", "SWR","000", "000", LOCATION_COUNTER, "000", "000000000000"));
								LOCATION_COUNTER++;
							}
						}
					}
					else {
						//ERROR: INSTRUCTIONS OF FORMAT 2 MUST CONTAIN TWO OPERANDS SEPARATED BY A COMMA
						currentline = (new Line(linecounter, "", "SWR","000", "000", LOCATION_COUNTER, "000", "000000000000"));
						currentline.addError(241);  
						LOCATION_COUNTER++;
					}
				}
				else {
					// ERROR: COMMAS CANT BE AT BEGINNING OR END INDEX
					currentline = (new Line(linecounter, "", "SWR","000", "000", LOCATION_COUNTER, "000", "000000000000"));
					currentline.addError(624);
					LOCATION_COUNTER++;
				}
			}
			else {
				//ERROR: INSTRUCTIONS OF FORMAT 2 MUST CONTAIN TWO OPERANDS SEPARATED BY A COMMA
				currentline = (new Line(linecounter, "", "SWR","000", "000", LOCATION_COUNTER, "000", "000000000000"));
				currentline.addError(241);    
				LOCATION_COUNTER++;
			}
		}
		else {
			// ERROR: INSTRUCTIONS HAS NO OPERANDS
			currentline = (new Line(linecounter, "", "SWR","000", "000", LOCATION_COUNTER, "000", "000000000000"));
			currentline.addError(500);
			LOCATION_COUNTER++;
		}
	}
	private static void parseAndCheckFormatThree (String[] tokens) {
		String operation = tokens[1];
		Boolean check = true;
		if ((operation.equals("CLRA") || operation.equals("CLRX"))) {
			if (tokens.length > 2) {
				//ERROR: INSTRUCTIONS CLRA AND CLRX USE NO OPERANDS: OPERANDS IGNORED

				// Add valid instruction to intermediate 
				if (!tokens[0].equals("") && isValidLabel(tokens[0])) {
					currentline = (new Line(linecounter, tokens[0], tokens[1], LOCATION_COUNTER, "000", "000000000000"));	
				}
				else {
					currentline = (new Line(linecounter, "", tokens[1], LOCATION_COUNTER, "000", "000000000000"));	
				}
				currentline.addError(250);
				LOCATION_COUNTER++;
				check = false;
			}
			else {

				// Add valid instruction to intermediate 
				if (!tokens[0].equals("") && isValidLabel(tokens[0])) {
					currentline = (new Line(linecounter, tokens[0], tokens[1], LOCATION_COUNTER, "000", "000000000000"));	
				}
				else {
					currentline = (new Line(linecounter, "", tokens[1], LOCATION_COUNTER, "000", "000000000000"));	
				}
				LOCATION_COUNTER++;
				check = false;
			}
		}
		if (check && tokens.length > 2) {
			String operand = tokens[2];
			if (operand.contains("(")) {
				// ERROR: NO INDEX VALUES ALLOWED IN OPERAND
				currentline = (new Line(linecounter, "", "SWR","000", "000", LOCATION_COUNTER, "000", "000000000000"));
				currentline.addError(638);
				LOCATION_COUNTER++;
			}
			else if (tokens[2].contains("!") || tokens[2].contains("?") || tokens[2].contains("#") || 
    				tokens[2].contains("_") || tokens[2].contains("/") || tokens[2].contains("\"") || 
    				tokens[2].contains(".") || tokens[2].contains("@") || tokens[2].contains("$") ||
    				tokens[2].contains("%") || tokens[2].contains("^") || tokens[2].contains("&") ||
    				tokens[2].contains("[") || tokens[2].contains("]") || tokens[2].contains("{") ||
    				tokens[2].contains("}") || tokens[2].contains("<") || tokens[2].contains(">")) {
				// ERROR: ADDRESS CONSTANT CONTAINS INVALID CHARACTERS 
				currentline = (new Line(linecounter, "", "SWR","000", "000", LOCATION_COUNTER, "000", "000000000000"));
				currentline.addError(621);
				LOCATION_COUNTER++;

			}
			else if (!operand.contains(",")) {
				if (operation.equals("DMP")) {
					if (operand.contains("=")) {
						// ERROR: FORMAT 3 INSTRUCTIONS CANNOT USE LITERALS 
						currentline = (new Line(linecounter, "", "SWR","000", "000", LOCATION_COUNTER, "000", "000000000000"));
						currentline.addError(639);
						LOCATION_COUNTER++;
					}
					else if (operand.contains("*")) {
						currentline = (new Line(linecounter, "", "SWR","000", "000", LOCATION_COUNTER, "000", "000000000000"));
						currentline.addError(640);
						LOCATION_COUNTER++;
					}
					else if ((operand.contains("+") || operand.contains("-")) && (operand.charAt(0) != '+' || operand.charAt(0) != '-')) {
						currentline = (new Line(linecounter, "", "SWR","000", "000", LOCATION_COUNTER, "000", "000000000000"));
						currentline.addError(650);
						LOCATION_COUNTER++;
					}
					else if((operand.charAt(0) == '+' || operand.charAt(0) == '-')) {
						if (operand.charAt(0) == '-') {
							// // ERROR: INVALID OPERAND FOR THE DMP OPERATION, MUST BE 1, 2, OR 3
							currentline = (new Line(linecounter, "", "SWR","000", "000", LOCATION_COUNTER, "000", "000000000000"));
							currentline.addError(251);
							LOCATION_COUNTER++;
						}
					}
					else if (isNumber(operand)) {
						int value = Integer.valueOf(operand);
						if (value < 1 || value > 3) {
							// ERROR: INVALID OPERAND FOR THE DMP OPERATION, MUST BE 1, 2, OR 3
							currentline = (new Line(linecounter, "", "SWR","000", "000", LOCATION_COUNTER, "000", "000000000000"));
							currentline.addError(251);
							LOCATION_COUNTER++;
						}
						else {
							String s_binary = Integer.toBinaryString(Integer.valueOf(operand));
							// VALID!


							// Add valid instruction to intermediate 
							if (!tokens[0].equals("") && isValidLabel(tokens[0])) {
								currentline = (new Line(linecounter, tokens[0], tokens[1], extendBinary(s_binary,3), LOCATION_COUNTER, "000", extendBinary(s_binary, 12)));	
							}
							else {
								currentline = (new Line(linecounter, "", tokens[1], extendBinary(s_binary,3), LOCATION_COUNTER, "000", extendBinary(s_binary, 12)));	
							}

							// Update symbol table if symbol is present
							if (SYMBOL_TABLE.symbolIsDefined(tokens[0])) {
								currentline.addError(675);
							}
							else if (!tokens[0].equals("") && isValidLabel(tokens[0])) {
								updateSymbolTable(tokens);
							}
							else if (!tokens[0].equals("")){
								checkForValidLabel(tokens[0]);
							}
							LOCATION_COUNTER++;
						}
					}
					else if (SYMBOL_TABLE.symbolIsDefined(operand)) {
						int value = Integer.valueOf(SYMBOL_TABLE.getString(operand));
						if (value >= 1 && value <= 3) {
							String s_binary = extendBinary(Integer.toBinaryString(value), 12);
							String s_field_hex = Integer.toHexString(value);
							// VALID!


							// Add valid instruction to intermediate 
							if (!tokens[0].equals("") && isValidLabel(tokens[0])) {
								currentline = (new Line(linecounter, tokens[0], tokens[1], extendBinary(s_binary,3), LOCATION_COUNTER, "000", extendBinary(s_binary, 12)));	
							}
							else {
								currentline = (new Line(linecounter, "", tokens[1], extendBinary(s_binary,3), LOCATION_COUNTER, "000", extendBinary(s_binary, 12)));	
							}

							// Update symbol table if symbol is present
							if (SYMBOL_TABLE.symbolIsDefined(tokens[0])) {
								currentline.addError(675);
							}
							else if (!tokens[0].equals("") && isValidLabel(tokens[0])) {
								updateSymbolTable(tokens);
							}
							else if (!tokens[0].equals("")){
								checkForValidLabel(tokens[0]);
							}
							LOCATION_COUNTER++;
						}
						else {
							// ERROR: MUST BE 1, 2, 3
							currentline = (new Line(linecounter, "", "SWR","000", "000", LOCATION_COUNTER, "000", "000000000000"));
							currentline.addError(646);
							LOCATION_COUNTER++;
						}
					}
					else {
						// ERROR: MUST BE 1, 2, 3
						currentline = (new Line(linecounter, "", "SWR","000", "000", LOCATION_COUNTER, "000", "000000000000"));
						currentline.addError(650);
						LOCATION_COUNTER++;
					}
				}
				
				if (operation.equals("HLT")) {
					if (isNumber(operand)) {
						if (!(Double.valueOf(operand) >= 0) || !(Double.valueOf(operand) <= 4095)) {
							//ERROR: INVALID S(X) USED, MUST BE IN RANGE OF 0-4095
							currentline = (new Line(linecounter, "", "SWR","000", "000", LOCATION_COUNTER, "000", "000000000000"));
							currentline.addError(231);
							LOCATION_COUNTER++;
						}
						else {
							String s_binary = extendBinary(Integer.toBinaryString(Integer.valueOf(operand)), 12);
							// VALID!!!
							
							// Add valid instruction to intermediate 
							if (!tokens[0].equals("") && isValidLabel(tokens[0])) {
								currentline = (new Line(linecounter, tokens[0], tokens[1] , extendBinary(Integer.toBinaryString(Integer.valueOf(operand)), 3), LOCATION_COUNTER, "000", s_binary));	
							}
							else {
								currentline = (new Line(linecounter, "", tokens[1] , extendBinary(Integer.toBinaryString(Integer.valueOf(operand)), 3), LOCATION_COUNTER, "000", s_binary));	
							}
							
							if (SYMBOL_TABLE.symbolIsDefined(tokens[0])) {
								currentline.addError(675);
							}
							else if (!tokens[0].equals("") && isValidLabel(tokens[0])) {
								updateSymbolTable(tokens);
							}
							else if (!tokens[0].equals("")){
								checkForValidLabel(tokens[0]);
							}
							LOCATION_COUNTER++;
						}
					}
					else if (operand.contains("*")) {
						// ERROR: INSTRUCTIONS OF FORMAT 3 ARE NOT ALLOWED TO USE STAR NOTATION
						currentline = (new Line(linecounter, "", "SWR","000", "000", LOCATION_COUNTER, "000", "000000000000"));
						currentline.addError(263);
						LOCATION_COUNTER++;
					}
					else if (operand.contains("=")) {
						// ERROR: NO LITERALS
						currentline = (new Line(linecounter, "", "SWR","000", "000", LOCATION_COUNTER, "000", "000000000000"));
						currentline.addError(639);
						LOCATION_COUNTER++;
					}
					else if (operand.contains("+") || (operand.contains("-") && operand.charAt(0) != '-')) {
						// ERROR: FORMAT 3 CANNOT CONTAIN EXPRESSIONS
						currentline = (new Line(linecounter, "", "SWR","000", "000", LOCATION_COUNTER, "000", "000000000000"));
						currentline.addError(647);
						LOCATION_COUNTER++;
					}
					else if (operand.charAt(0) == '-') {
						currentline = (new Line(linecounter, "", "SWR","000", "000", LOCATION_COUNTER, "000", "000000000000"));
						currentline.addError(231);
						LOCATION_COUNTER++;
					}
					else if (SYMBOL_TABLE.symbolIsDefined(operand) && !SYMBOL_TABLE.getUsage(operand).equals("EXT")) {
						String s_binary = extendBinary(Integer.toBinaryString(Integer.valueOf(SYMBOL_TABLE.getString(operand))),12);
						String s_hex = Integer.toHexString(Integer.valueOf(SYMBOL_TABLE.getString(operand)));
						// VALID!!
						
						// Add valid instruction to intermediate 
						if (!tokens[0].equals("") && isValidLabel(tokens[0])) {
							currentline = (new Line(linecounter, tokens[0], tokens[1] , extendBinary(Integer.toBinaryString(Integer.valueOf(SYMBOL_TABLE.getString(operand))),3), LOCATION_COUNTER, "000", s_binary));	
						}
						else {
							currentline = (new Line(linecounter, "", tokens[1] , extendBinary(Integer.toBinaryString(Integer.valueOf(SYMBOL_TABLE.getString(operand))),3), LOCATION_COUNTER, "000", s_binary));	
						}
						
						if (SYMBOL_TABLE.symbolIsDefined(tokens[0])) {
							currentline.addError(675);
						}
						else if (!tokens[0].equals("") && isValidLabel(tokens[0])) {
							updateSymbolTable(tokens);
						}
						else if (!tokens[0].equals("")){
							checkForValidLabel(tokens[0]);
						}
						LOCATION_COUNTER++;
					}
					else if (SYMBOL_TABLE.symbolIsDefined(operand) && SYMBOL_TABLE.getUsage(operand).equals("EXT")) {
						// ERROR: HLT DOES NOT ALLOW THE USE OF EXTERNAL SYMBOLS AS OPERANDS
						currentline = (new Line(linecounter, "", "SWR","000", "000", LOCATION_COUNTER, "000", "000000000000"));
						currentline.addError(641);
						LOCATION_COUNTER++;
					}
					else {
						// ERROR: MUST BE PREVIOUSLY DEFINED
						currentline = (new Line(linecounter, "", "SWR","000", "000", LOCATION_COUNTER, "000", "000000000000"));
						currentline.addError(150);
						LOCATION_COUNTER++;
					}
				}
			}
			else {
				//ERROR: INSTRUCTIONS OF FORMAT 3 CONTAIN ONLY ONE OPERAND
				currentline = (new Line(linecounter, "", "SWR","000", "000", LOCATION_COUNTER, "000", "000000000000"));
				currentline.addError(242);
				LOCATION_COUNTER++;
			}
		}
		else if (check) {
			if (operation.equals("CLRA") || operation.equals("CLRX")) {
				// VALID!!
				
				// Add valid instruction to intermediate 
				if (!tokens[0].equals("") && isValidLabel(tokens[0])) {
					currentline = (new Line(linecounter, tokens[0], tokens[1], LOCATION_COUNTER, "000", "000000000000"));	
				}
				else {
					currentline = (new Line(linecounter, "", tokens[1], LOCATION_COUNTER, "000", "000000000000"));	
				}
				
				if (SYMBOL_TABLE.symbolIsDefined(tokens[0])) {
					currentline.addError(675);
				}
				else if (!tokens[0].equals("") && isValidLabel(tokens[0])) {
					updateSymbolTable(tokens);
				}
				else if (!tokens[0].equals("")){
					checkForValidLabel(tokens[0]);
				}
				LOCATION_COUNTER++;
			}
			else {
				// ERROR: INSTRUCTION DOES NOT CONTAINS NO REQUIRED OPERANDS
				currentline = (new Line(linecounter, "", "SWR","000", "000", LOCATION_COUNTER, "000", "000000000000"));
				currentline.addError(500);
				LOCATION_COUNTER++;
			}
		}
	}
	private static void parseAndCheckFormatFive (String[] tokens) {
		Boolean valid_d = true, valid_mem = true, valid_t = true, valid_l = true;
		if (tokens.length > 2) {
			String operand = tokens[2];
			if (operand.contains(",")) {
				String[] operands = operand.split(",");
				if (operand.charAt(0) == ',' || operand.charAt(operand.length()-1) == ',') {
					// ERROR: COMMAS AT FRONT AND BACK
					currentline = (new Line(linecounter, "", "SWR","000", "000", LOCATION_COUNTER, "000", "000000000000"));
					currentline.addError(624);
					LOCATION_COUNTER++;
					valid_d = false;
				}
				// Check if there are four operands
				else if (operands.length == 4) {
					String d_value = operands[0];
					String memory = operands[1];
					String t_value = operands[2];
					String l_value = operands[3];
					// Check D operand
					if (isNumber(operands[0])) {
						if (!operands[0].equals("1") && !operands[0].equals("2")) {
							//ERROR: THE D OPERAND MUST BE EITHER 1 OR 2
							currentline = (new Line(linecounter, "", "SWR","000", "000", LOCATION_COUNTER, "000", "000000000000"));
							currentline.addError(260);
							LOCATION_COUNTER++;
							valid_d = false;
						}
					}
					else if (operands[0].contains("*")) {
						//ERROR: THE D OPERAND CANT HAVE STAR
						currentline = (new Line(linecounter, "", "SWR","000", "000", LOCATION_COUNTER, "000", "000000000000"));
						currentline.addError(659);
						LOCATION_COUNTER++;
						valid_d = false;
					}
					else if (operands[0].contains("(") || operands[0].contains(")")) {
						//ERROR: THE D OPERAND CANT HAVE INDEX
						currentline = (new Line(linecounter, "", "SWR","000", "000", LOCATION_COUNTER, "000", "000000000000"));
						currentline.addError(662);
						LOCATION_COUNTER++;
						valid_d = false;
					}
					else if (SYMBOL_TABLE.symbolIsDefined(operands[0])) {
						d_value = SYMBOL_TABLE.getString(operands[0]);
						if (!SYMBOL_TABLE.getString(operands[0]).equals("1") && !SYMBOL_TABLE.getString(operands[0]).equals("2")) {
							//ERROR: THE D OPERAND MUST BE EITHER 1 OR 2
							currentline = (new Line(linecounter, "", "SWR","000", "000", LOCATION_COUNTER, "000", "000000000000"));
							currentline.addError(260);
							LOCATION_COUNTER++;
							valid_d = false;
						}
					}
					else if (valid_d) {
						//ERROR: THE D OPERAND MUST BE EITHER 1 OR 2
						currentline = (new Line(linecounter, "", "SWR","000", "000", LOCATION_COUNTER, "000", "000000000000"));
						currentline.addError(260);
						LOCATION_COUNTER++;
						valid_d = false;
					}
					
					// Check S(X) operand
					if (valid_d && isValidMemoryReference(operands[1])) {
						if (tokens[1].equals("RDV")) {
							if (operands[1].contains("=")) {
								currentline = (new Line(linecounter, "", "SWR","000", "000", LOCATION_COUNTER, "000", "000000000000"));
								currentline.addError(652);
								LOCATION_COUNTER++;
								valid_mem = false;
							}
						}
					}
					else if (valid_d && !isValidMemoryReference(operands[1])) {
						currentline = (new Line(linecounter, "", "SWR","000", "000", LOCATION_COUNTER, "000", "000000000000"));
						LOCATION_COUNTER++;
						checkMemoryReferenceSyntax(operands[1]);
						valid_mem = false;
					}

					// Check T operand
					if (valid_d && valid_mem && isNumber(operands[2])) {
						if (!operands[2].equals("1") && !operands[2].equals("2")) {
							//ERROR: THE T OPERAND MUST BE EITHER 1 OR 2
							currentline = (new Line(linecounter, "", "SWR","000", "000", LOCATION_COUNTER, "000", "000000000000"));
							currentline.addError(261);
							LOCATION_COUNTER++;
							valid_t = false;
						}
					}
					else if (valid_d && valid_mem && operands[2].contains("*")) {
						//ERROR: THE T OPERAND CANT HAVE STAR
						currentline = (new Line(linecounter, "", "SWR","000", "000", LOCATION_COUNTER, "000", "000000000000"));
						currentline.addError(660);
						LOCATION_COUNTER++;
						valid_d = false;
					}
					else if (valid_d && valid_mem && (operands[2].contains("(") || operands[2].contains(")"))) {
						//ERROR: THE T OPERAND CANT HAVE INDEX
						currentline = (new Line(linecounter, "", "SWR","000", "000", LOCATION_COUNTER, "000", "000000000000"));
						currentline.addError(663);
						LOCATION_COUNTER++;
						valid_d = false;
					}
					else if (SYMBOL_TABLE.symbolIsDefined(operands[2])) {
						t_value = SYMBOL_TABLE.getString(operands[2]);

						if (!SYMBOL_TABLE.getString(operands[0]).equals("1") && !SYMBOL_TABLE.getString(operands[0]).equals("2")) {
							//ERROR: THE T OPERAND MUST BE EITHER 1 OR 2
							currentline = (new Line(linecounter, "", "SWR","000", "000", LOCATION_COUNTER, "000", "000000000000"));
							currentline.addError(261);
							LOCATION_COUNTER++;
							valid_t = false;
						}
					}
					else if (valid_d && valid_mem){
						//ERROR: THE T OPERAND MUST BE EITHER 1 OR 2
						currentline = (new Line(linecounter, "", "SWR","000", "000", LOCATION_COUNTER, "000", "000000000000"));
						currentline.addError(261);
						LOCATION_COUNTER++;
						valid_t = false;
					}

					// Check L operand
					if (valid_d && valid_mem && valid_t && isNumber(operands[3])) {
						if (!(Double.valueOf(operands[3]) >= 0) || !(Double.valueOf(operands[3]) <= 4095)) {
							//ERROR: INVALID L VALUE USED, MUST BE IN RANGE OF 0-4095
							currentline = (new Line(linecounter, "", "SWR","000", "000", LOCATION_COUNTER, "000", "000000000000"));
							currentline.addError(233);
							LOCATION_COUNTER++;
							valid_l = false;
						}
					}
					else if (valid_d && valid_mem && valid_t && operands[3].contains("*")) {
						//ERROR: THE L OPERAND CANT HAVE STAR
						currentline = (new Line(linecounter, "", "SWR","000", "000", LOCATION_COUNTER, "000", "000000000000"));
						currentline.addError(661);
						LOCATION_COUNTER++;
						valid_d = false;
					}
					else if (valid_d && valid_mem && valid_t && (operands[3].contains("(") || operands[3].contains(")"))) {
						//ERROR: THE L OPERAND CANT HAVE INDEX
						currentline = (new Line(linecounter, "", "SWR","000", "000", LOCATION_COUNTER, "000", "000000000000"));
						currentline.addError(664);
						LOCATION_COUNTER++;
						valid_d = false;
					}
					else if (valid_d && valid_mem && valid_t && SYMBOL_TABLE.symbolIsDefined(operands[3])) {
						l_value = SYMBOL_TABLE.getString(operands[3]);
						int value = Integer.valueOf(SYMBOL_TABLE.getString(operands[3]));
						if (!(value >= 0) || !(value <= 4095)) {
							//ERROR: INVALID L VALUE USED, MUST BE IN RANGE OF 0-4095
							currentline = (new Line(linecounter, "", "SWR","000", "000", LOCATION_COUNTER, "000", "000000000000"));
							currentline.addError(233);
							LOCATION_COUNTER++;
							valid_l = false;
						}
					}
					else if (valid_d && valid_mem && valid_t){
						//ERROR: INVALID L VALUE USED, MUST BE IN RANGE OF 0-4095
						currentline = (new Line(linecounter, "", "SWR","000", "000", LOCATION_COUNTER, "000", "000000000000"));
						currentline.addError(233);
						LOCATION_COUNTER++;
						valid_l = false;
					}

					// Print correct breakdown
					if (valid_d && valid_mem && valid_t && valid_l) {
						String s_field_binary = "???";
						String s_field_hex = "???";
						String x_field_binary = "000";
						String memory_ref = operands[1];
						String dev_bin = extendBinary(Integer.toBinaryString(Integer.valueOf(d_value)),3);
						String type_bin = extendBinary(Integer.toBinaryString(Integer.valueOf(t_value)),2);
						String length_bin = extendBinary(Integer.toBinaryString(Integer.valueOf(l_value)),4);
						String memory_symbol = "";
						if (memory_ref.contains("(") && memory_ref.contains(")")) {
							s_field_hex = getSHex(memory_ref).toUpperCase();
							if (!s_field_hex.equals("???")) {
								s_field_binary = extendBinary(Integer.toBinaryString(Integer.parseInt(getSHex(memory_ref),16)), 12);
							}
						
							x_field_binary = getXBinary(memory_ref);
							int i = 0;
							while (memory_ref.charAt(i) != '(') {
								i++;
							}
							memory_ref = memory_ref.substring(0,i);
						}
						else if (isNumber(memory_ref)) {
							s_field_hex = Integer.toHexString(Integer.valueOf(memory_ref)).toUpperCase();
							s_field_binary = extendBinary(Integer.toBinaryString(Integer.valueOf(memory_ref)), 12);
							x_field_binary = "000";
						}
						else if (isLiteral(memory_ref)) {
							s_field_hex = "???";
							x_field_binary = "000";
							String literal_value = memory_ref.substring(1);
							if (!SYMBOL_TABLE.symbolIsDefined(memory_ref)) {
								SYMBOL_TABLE.add(new Symbol(memory_ref, 0, "LITERAL", literal_value));
							}
						}
						else if (memory_ref.charAt(0) == '*' && memory_ref.length() == 1) {
							s_field_hex = Integer.toHexString(LOCATION_COUNTER);
							s_field_binary = extendBinary(Integer.toBinaryString(LOCATION_COUNTER), 12);
							x_field_binary = "000";
						}
						else if (memory_ref.charAt(0) == '*' && (memory_ref.charAt(1) == '+' || memory_ref.charAt(1) == '-')) {
							int result = 0;
							if (isNumber(memory_ref.substring(2))) {
								if (memory_ref.charAt(1) == '-') {
									result = LOCATION_COUNTER - Integer.valueOf(memory_ref.substring(2));
								}
								else {
									result = LOCATION_COUNTER + Integer.valueOf(memory_ref.substring(2));
								}
							}
							else {
								String symbol_value = SYMBOL_TABLE.getString(memory_ref.substring(2));
								if (memory_ref.charAt(1) == '-') {
									result = LOCATION_COUNTER - Integer.valueOf(symbol_value);
								}
								else {
									result = LOCATION_COUNTER + Integer.valueOf(symbol_value);
								}
							}
							
							
							s_field_hex = Integer.toHexString(result);
							s_field_binary = extendBinary(Integer.toBinaryString(result), 12);
							x_field_binary = "000";
						}
						else if (SYMBOL_TABLE.symbolIsDefined(memory_ref)) {
							int symbol_value = Integer.valueOf(SYMBOL_TABLE.getLocation(memory_ref));
							s_field_hex = Integer.toHexString(symbol_value).toUpperCase();
							s_field_binary = extendBinary(Integer.toBinaryString(symbol_value), 12);
							x_field_binary = "000";
						}


						// Add valid instruction to intermediate 
						if (!tokens[0].equals("") && isValidLabel(tokens[0])) {
							currentline = (new Line(linecounter, tokens[0], tokens[1] , dev_bin, memory_ref, type_bin, length_bin, LOCATION_COUNTER, extendBinary(x_field_binary, 3), s_field_binary));	
						}
						else {
							currentline = (new Line(linecounter, "", tokens[1] , dev_bin, memory_ref, type_bin, length_bin, LOCATION_COUNTER, extendBinary(x_field_binary, 3), s_field_binary));	
						}
						
						if (SYMBOL_TABLE.symbolIsDefined(tokens[0])) {
							currentline.addError(675);
						}
						else if (!tokens[0].equals("") && isValidLabel(tokens[0])) {
							updateSymbolTable(tokens);
						}
						else if (!tokens[0].equals("")){
							checkForValidLabel(tokens[0]);
						}
						LOCATION_COUNTER++;
					}

				}
				else {
					//ERROR: INSTRUCTIONS OF FORMAT 5 CONTAIN EXACTLY FOUR OPERANDS
					currentline = (new Line(linecounter, "", "SWR","000", "000", LOCATION_COUNTER, "000", "000000000000"));
					currentline.addError(243);
					LOCATION_COUNTER++;
				}
			}
			else {
				//ERROR: INSTRUCTIONS OF FORMAT 5 CONTAIN FOUR OPERANDS SEPARATED BY COMMAS
				currentline = (new Line(linecounter, "", "SWR","000", "000", LOCATION_COUNTER, "000", "000000000000"));
				currentline.addError(244);
				LOCATION_COUNTER++;
			}
		}
		else {
			currentline = (new Line(linecounter, "", "SWR","000", "000", LOCATION_COUNTER, "000", "000000000000"));
			currentline.addError(500);
			LOCATION_COUNTER++;
		}
	}
	private static void parseAndCheckFormatSix (String[] tokens) {
		if (tokens.length > 2) {
			String operand = tokens[2];
			if (operand.contains("=")) {
				// ERROR: FORMAT 6 OPERANDS CANNOT USE LITERALS
				currentline = (new Line(linecounter, "", "SWR","000", "000", LOCATION_COUNTER, "000", "000000000000"));
				currentline.addError(653);
				LOCATION_COUNTER++;
			}
			else if (operand.contains("*")) {
				// ERROR: FORMAT 6 OPERANDS CANNOT USE STAR NOTATION
				currentline = (new Line(linecounter, "", "SWR","000", "000", LOCATION_COUNTER, "000", "000000000000"));
				currentline.addError(654);
				LOCATION_COUNTER++;
			}
			else if (operand.contains("+") || operand.contains("-")) {
				// ERROR: FORMAT 6 OPERANDS CANNOT USE EXPRESSION
				currentline = (new Line(linecounter, "", "SWR","000", "000", LOCATION_COUNTER, "000", "000000000000"));
				currentline.addError(665);
				LOCATION_COUNTER++;
			}
			else if (operand.contains(",")) {
				if (operand.charAt(0) != ',' && operand.charAt(operand.length()-1) != ',') {
					if (operand.length() > 2) {
						if (operand.split(",").length == 2) {
							if (!operand.contains("(") && !operand.contains(")")) {
								Boolean valid_pg1 = false, valid_pg2 = false;
								String pg1 = operand.split(",")[0];
								String pg2 = operand.split(",")[1];

								// Check if valid pg1 operand is used
								if (isNumber(pg1) && (Integer.valueOf(pg1) >= 0 && Integer.valueOf(pg1) <= 3)) {
									valid_pg1 = true;
								}
								else if (SYMBOL_TABLE.symbolIsDefined(pg1) && (Integer.valueOf(SYMBOL_TABLE.getString(pg1)) >= 0 && Integer.valueOf(SYMBOL_TABLE.getString(pg1)) <= 3)){
									valid_pg1 = true;
									pg1 = SYMBOL_TABLE.getString(pg1);

								}
								else {
									currentline = (new Line(linecounter, "", "SWR","000", "000", LOCATION_COUNTER, "000", "000000000000"));
									currentline.addError(122);
									LOCATION_COUNTER++;
								}

								// Check if valid pg2 operand is used
								if (valid_pg1 && isNumber(pg2) && (Double.valueOf(pg2) >= 0 && Double.valueOf(pg2) <= 3)) {
									valid_pg2 = true;
								}
								else if (SYMBOL_TABLE.symbolIsDefined(pg2) && (Integer.valueOf(SYMBOL_TABLE.getString(pg2)) >= 0 && Integer.valueOf(SYMBOL_TABLE.getString(pg2)) <= 3)){
									valid_pg2 = true;
									pg2 = SYMBOL_TABLE.getString(pg2);
								}
								else if (valid_pg1){
									currentline = (new Line(linecounter, "", "SWR","000", "000", LOCATION_COUNTER, "000", "000000000000"));
									currentline.addError(123);
									LOCATION_COUNTER++;
								}

								// Print breakdown if both pages of operand are valid
								if (valid_pg1 && valid_pg2) {
									String pg1_bin = extendBinary(Integer.toBinaryString(Integer.valueOf(pg1)), 2);
									String pg2_bin = extendBinary(Integer.toBinaryString(Integer.valueOf(pg2)), 2);


									// Add valid instruction to intermediate 
									if (!tokens[0].equals("") && isValidLabel(tokens[0])) {
										currentline = (new Line(linecounter, tokens[0], tokens[1],pg1_bin, pg2_bin, LOCATION_COUNTER, "000", "000000000000"));	
									}
									else {
										currentline = (new Line(linecounter, "", tokens[1],pg1_bin, pg2_bin, LOCATION_COUNTER, "000", "000000000000"));	
									}

									if (SYMBOL_TABLE.symbolIsDefined(tokens[0])) {
										currentline.addError(675);
									}
									else if (!tokens[0].equals("") && isValidLabel(tokens[0])) {
										updateSymbolTable(tokens);
									}
									else if (!tokens[0].equals("")){
										checkForValidLabel(tokens[0]);
									}
									LOCATION_COUNTER++;
								}
							}
							else {
								// ERROR: FORMAT 6 INSTRUCTIONS CANNOT USE INDEXED MEMORY REFERENCES
								currentline = (new Line(linecounter, "", "SWR","000", "000", LOCATION_COUNTER, "000", "000000000000"));
								currentline.addError(645);
								LOCATION_COUNTER++;
							}
						}
						else {
							// ERROR: MUST BE 2 OPERANDS SEPARATED BY COMMA
							currentline = (new Line(linecounter, "", "SWR","000", "000", LOCATION_COUNTER, "000", "000000000000"));
							currentline.addError(245);
							LOCATION_COUNTER++;
						}
					}
					else {
						// ERROR: MUST BE 2 OPERANDS SEPARATED BY COMMA
						currentline = (new Line(linecounter, "", "SWR","000", "000", LOCATION_COUNTER, "000", "000000000000"));
						currentline.addError(245);
						LOCATION_COUNTER++;
					}
				}
				else {
					// ERROR: COMMAS CANNOT BE THE FIRST OR LAST CHARACTER OF OPERAND
					currentline = (new Line(linecounter, "", "SWR","000", "000", LOCATION_COUNTER, "000", "000000000000"));
					currentline.addError(624);
					LOCATION_COUNTER++;
				}
			}
			else {
				//ERROR: INSTRUCTIONS OF FORMAT 6 CONTAIN TWO OPERANDS SEPARATED BY A COMMA
				currentline = (new Line(linecounter, "", "SWR","000", "000", LOCATION_COUNTER, "000", "000000000000"));
				currentline.addError(245);
				LOCATION_COUNTER++;
			}
			
		}
		else {
			// ERROR: INSTRUCTION HAS NO REQUIRED OPERANDS, OPERAND FIELD IS EMPTY
			currentline = (new Line(linecounter, "", "SWR","000", "000", LOCATION_COUNTER, "000", "000000000000"));
			currentline.addError(500);	
			LOCATION_COUNTER++;
		}
	}

	private static void parseAndCheckFormatSeven (String[] tokens) {
		if (tokens.length > 2) {
			Boolean valid_r1 = false, valid_r2 = false;
			String operand = tokens[2];

			if (operand.contains("(")) {
				currentline = (new Line(linecounter, "", "SWR","000", "000", LOCATION_COUNTER, "000", "000000000000"));
				currentline.addError(642);
				LOCATION_COUNTER++;
			}
			else if (operand.contains("=")) {
				currentline = (new Line(linecounter, "", "SWR","000", "000", LOCATION_COUNTER, "000", "000000000000"));
				currentline.addError(643);
				LOCATION_COUNTER++;
			}
			else if (operand.contains("*")) {
				currentline = (new Line(linecounter, "", "SWR","000", "000", LOCATION_COUNTER, "000", "000000000000"));
				currentline.addError(644);
				LOCATION_COUNTER++;
			}
			else if (operand.contains("+") || operand.contains("-")) {
				currentline = (new Line(linecounter, "", "SWR","000", "000", LOCATION_COUNTER, "000", "000000000000"));
				currentline.addError(631);
				LOCATION_COUNTER++;
			}
			else if (tokens[2].contains("!") || tokens[2].contains("?") || tokens[2].contains("#") || 
    				tokens[2].contains("_") || tokens[2].contains("/") || tokens[2].contains("\"") || 
    				tokens[2].contains(".") || tokens[2].contains("@") || tokens[2].contains("$") ||
    				tokens[2].contains("%") || tokens[2].contains("^") || tokens[2].contains("&") ||
    				tokens[2].contains("[") || tokens[2].contains("]") || tokens[2].contains("{") ||
    				tokens[2].contains("}") || tokens[2].contains("<") || tokens[2].contains(">")) {
				currentline = (new Line(linecounter, "", "SWR","000", "000", LOCATION_COUNTER, "000", "000000000000"));
				currentline.addError(621);
				LOCATION_COUNTER++;
			}
			else if (operand.contains(",")) {
				if (operand.charAt(0) != ',' && operand.charAt(operand.length()-1) != ',') {
					if (operand.split(",").length == 2) {
						String r1 = operand.split(",")[0];
						String r2 = operand.split(",")[1];

						// Check if valid r1operand is used
						if (isNumber(r1)) {
							if (Double.valueOf(r1) >= 0 && Double.valueOf(r1) <= 7) {
								valid_r1 = true;
							}
							else {
								valid_r2 = false;
							}
						}
						else if (!SYMBOL_TABLE.symbolIsDefined(r1)) {
							// ERROR: SYMBOL MUST BE PREVIOUSLY EQUATED BEFORE USED AS A REGISTER OPERAND
							currentline = (new Line(linecounter, "", "SWR","000", "000", LOCATION_COUNTER, "000", "000000000000"));
							currentline.addError(228);
							LOCATION_COUNTER++;

						}
						else if (SYMBOL_TABLE.symbolIsDefined(r1)) {
							r1 = SYMBOL_TABLE.getString(r1);
							if (Integer.valueOf(r1) >= 0 && Integer.valueOf(r1) <= 7) {
								valid_r1 = true;
							}
							else {
								currentline = (new Line(linecounter, "", "SWR","000", "000", LOCATION_COUNTER, "000", "000000000000"));
								currentline.addError(235);
								valid_r1 = false;
							}
						}
						else {
							currentline = (new Line(linecounter, "", "SWR","000", "000", LOCATION_COUNTER, "000", "000000000000"));
							currentline.addError(235);
							valid_r1 = false;
						}

						// Check if valid r2 operand is used
						if (valid_r1 && isNumber(r2)) {
							if (Double.valueOf(r2) >= 0 && Double.valueOf(r2) <= 7) {
								valid_r2 = true;
							}
							else {
								valid_r2 = false;
							}
						}
						else if (valid_r1 && !SYMBOL_TABLE.symbolIsDefined(r2)) {
							// ERROR: SYMBOL MUST BE PREVIOUSLY EQUATED BEFORE USED AS A REGISTER OPERAND
							currentline = (new Line(linecounter, "", "SWR","000", "000", LOCATION_COUNTER, "000", "000000000000"));
							currentline.addError(228);
							LOCATION_COUNTER++;
						}
						else if (valid_r1 && SYMBOL_TABLE.symbolIsDefined(r2)) {
							r2 = SYMBOL_TABLE.getString(r2);
							if (Integer.valueOf(r2) >= 0 && Integer.valueOf(r2) <= 7) {
								valid_r2 = true;
							}
							else {
								currentline = (new Line(linecounter, "", "SWR","000", "000", LOCATION_COUNTER, "000", "000000000000"));
								currentline.addError(236);
								valid_r2 = false;
							}
						}
						else if (valid_r1){
							currentline = (new Line(linecounter, "", "SWR","000", "000", LOCATION_COUNTER, "000", "000000000000"));
							currentline.addError(236);
							valid_r2 = false;
						}

						// Print breakdown if both register operands are valid
						if (valid_r1 && valid_r2) {
							String r1_bin = extendBinary(Integer.toBinaryString(Integer.valueOf(r1)),3);
							String r2_bin = extendBinary(Integer.toBinaryString(Integer.valueOf(r2)),3);


							// Add valid instruction to intermediate 
							if (!tokens[0].equals("") && isValidLabel(tokens[0])) {
								currentline = (new Line(linecounter, tokens[0], tokens[1],r1_bin, r2_bin, LOCATION_COUNTER, "000", "000000000000"));	
							}
							else {
								currentline = (new Line(linecounter, "", tokens[1],r1_bin, r2_bin, LOCATION_COUNTER, "000", "000000000000"));	
							}

							if (SYMBOL_TABLE.symbolIsDefined(tokens[0])) {
								currentline.addError(675);
							}
							else if (!tokens[0].equals("") && isValidLabel(tokens[0])) {
								updateSymbolTable(tokens);
							}
							else if (!tokens[0].equals("")){
								checkForValidLabel(tokens[0]);
							}
							LOCATION_COUNTER++;

						}
					}
					else {
						// ERROR: FORMAT 7 ONLY HAS 2 OPERANDS
						currentline = (new Line(linecounter, "", "SWR","000", "000", LOCATION_COUNTER, "000", "000000000000"));
						currentline.addError(666);
						LOCATION_COUNTER++;
					}
				}
				else {
					// ERROR: COMMAS CANT BE AT BEGINNING OR END OF OPERAND FIELD
					currentline = (new Line(linecounter, "", "SWR","000", "000", LOCATION_COUNTER, "000", "000000000000"));
					currentline.addError(624);
					LOCATION_COUNTER++;
				}
			}
		}
		else {
			// ERROR: INSTRUCTION HAS NO OPERANDS
			currentline = (new Line(linecounter, "", "SWR","000", "000", LOCATION_COUNTER, "000", "000000000000"));
			currentline.addError(500);
			LOCATION_COUNTER++;
			
		}
	}
	
    private static void parseDirective (String[] tokens) {
        /*
         *  parseDirective takes the same array of tokens as before and outputs them in the correct format just like
         *  parseInstruction did but with a slightly different output 
         */
    	String directive = tokens[1];

    	if (directive.equals("STT")) {
    		parseAndCheckSTT(tokens);
    	}
    	else if (directive.equals("RESET")) {
    		parseAndCheckRESET(tokens);
    	}
    	else if (directive.equals("EQU")) {
    		parseAndCheckEQU(tokens);
    	}
    	else if (directive.equals("EQUE")) {
    		parseAndCheckEQUe(tokens);
    	}
    	else if (directive.equals("ENT")) {
    		parseAndCheckENT(tokens);
    	}
    	else if (directive.equals("EXT")) {
    		parseAndCheckEXT(tokens);
    	}
    	else if (directive.equals("END")) {
    		parseAndCheckEND(tokens);
    	}
    	else if (directive.equals("AEXS")) {
    		parseAndCheckAEXS(tokens);
    	}
    	else if (directive.equals("SKS")) {
    		parseAndCheckSKS(tokens);
    	}
    	else if (directive.equals("CHC")) {
    		parseAndCheckCHC(tokens);
    	}
    	else if (directive.equals("NUM")) {
    		parseAndCheckNum(tokens);
    	}
    	else if (directive.equals("ADC")) {
    		parseAndCheckADC(tokens);
    	}
    	else if (directive.equals("ADCE")) {
    		parseAndCheckADCe(tokens);
    	}
    	else if (directive.equals("NOP")) {
    		parseAndCheckNOP(tokens);
    	}
    }

    private static void parseAndCheckSTT (String[] tokens) {
    	// Check if required label is present        
    	if (!tokens[0].equals("")) {
    		// Check label for correct formatting
    		checkForValidLabel(tokens[0]);
    		if (!isValidLabel(tokens[0])) {
    			valid_start = false;
    			continue_assembly = false;
    			outputPrint("ERROR :  **FATAL** PROGRAM LABEL DOES NOT HAVE VALID SYNTAX: ASSEMBLY WILL BE STOPPED");

    		}
    		else {
    			updateSymbolTable(tokens);
    		}
    	}
    	else {
    		//ERROR: THIS CALL TO STT DOES NOT HAVE THE REQUIRED LABEL
    		outputPrint("ERROR 110 :  **FATAL** THIS CALL TO STT DOES NOT HAVE THE REQUIRED LABEL: ASSEMBLY WILL BE STOPPED");

    		valid_start = false;
    		continue_assembly = false;

    	}

    	// Check operands for correct formatting and bounds
    	if (valid_start && tokens.length > 2) {
    		if (valid_start && tokens[2].contains(",")) {
    			if (tokens[2].charAt(0) != ',' && tokens[2].charAt(tokens[2].length()-1) != ',') {

    				if (tokens[2].split(",")[0].charAt(0) == '-') {
    					outputPrint("ERROR 003 :  **FATAL** START PAGE OUT OF BOUNDS, MUST BE IN RANGE OF 0-4095: ASSEMBLY WILL BE STOPPED");

    					valid_start = false;
    					continue_assembly = false;

    				}
    				else if (tokens[2].split(",")[1].charAt(0) == '-') {
    					outputPrint("ERROR 000 :  **FATAL** START LOCATION OUT OF BOUNDS, MUST BE IN RANGE OF 0-4095: ASSEMBLY WILL BE STOPPED");
    					valid_start = false;
    					continue_assembly = false;

    				}
    				else if (isNumber(tokens[2].split(",")[0]) && isNumber(tokens[2].split(",")[1])) {
    					String page = tokens[2].split(",")[0];
    					String loc = tokens[2].split(",")[1];

    					// Check page number 
    					if (!(Double.valueOf(page) >= 0) || !(Double.valueOf(page) <= 3)) {
    						//ERROR: THIS CALL TO STT USES AN INVALID MEMORY PAGE NUMBER
    						outputPrint("ERROR 003 :  **FATAL** START PAGE OUT OF BOUNDS, MUST BE IN RANGE OF 0-4095: ASSEMBLY WILL BE STOPPED");
    						valid_start = false;
    						continue_assembly = false;

    					}

    					// Check memory start location
    					if (!(Double.valueOf(loc) >= 0) || !(Double.valueOf(loc) <= 4095)) {
    						//ERROR: THIS CALL TO STT USES A START LOCATION THAT IS OUT OF BOUNDS
    						outputPrint("ERROR 000 :  **FATAL** START LOCATION OUT OF BOUNDS, MUST BE IN RANGE OF 0-4095: ASSEMBLY WILL BE STOPPED");
    						valid_start = false;
    						continue_assembly = false;

    					}

    					// Print breakdown for STT
    					if (valid_start) {
    						START_PAGE = Integer.valueOf(page);
    						START_LOCATION = Integer.valueOf(loc);
    						LOCATION_COUNTER = Integer.valueOf(loc);
    						START_NAME = tokens[0];
    						currentline = (new Line(linecounter, tokens[0], "STT", page, loc, LOCATION_COUNTER, "000", "000000000000"));
    					}
    				}
    				else {
    					//ERROR: START LOCATION OPERAND FOR STT MUST BE A NUMERICAL VALUE ONLY
    					outputPrint("ERROR 004 :  **FATAL** START LOCATION OPERAND MUST BE A NUMERICAL VALUE: ASSEMBLY WILL BE STOPPED");
    					valid_start = false;
    					continue_assembly = false;

    				}
    			}
    			else {
    				//ERROR: STT HAS INVALID OPERAND FIELD
					outputPrint("ERROR :  **FATAL** STT HAS AN INVALID OPERAND FIELD: ASSEMBLY WILL BE STOPPED");
					valid_start = false;
					continue_assembly = false;
    			}
    		}
    		else if (valid_start) {
    			// ERROR: STT OPERATION MUST CONTAIN PAGE NUMBER AND START LOCATION
    			outputPrint("ERROR 007 :  **FATAL** STT OPERAND MUST CONTAIN A PAGE NUMBER AND START LOCATION: ASSEMBLY WILL BE STOPPED");
    			valid_start = false;
    			continue_assembly = false;
    		}
    	}
    	else if (valid_start){
    		// ERROR: STT OPERATION MUST CONTAIN PAGE NUMBER AND START LOCATION
			outputPrint("ERROR 007 :  **FATAL** STT OPERAND MUST CONTAIN A PAGE NUMBER AND START LOCATION: ASSEMBLY WILL BE STOPPED");
			valid_start = false;
			continue_assembly = false;
    	}

    }
    
    private static void parseAndCheckRESET (String[] tokens) {
    	Boolean valid_reset = true;
    	int reset_location = 0;
    	
        // Check if required label is present        
        if (!tokens[0].equals("")) {
            
            // Check label for correct formatting
            if (!isValidLabel(tokens[0])) {
				currentline = (new Line(linecounter, "", "SWR","000", "000", LOCATION_COUNTER, "000", "000000000000"));
				LOCATION_COUNTER++;
				
				// Print label error messages
                checkForValidLabel(tokens[0]);
                valid_reset = false;
            }

        }
        else if (tokens.length > 2) {
            //ERROR: DIRECTIVE DOES NOT HAVE REQUIRED LABEL
			currentline = (new Line(linecounter, "", "SWR","000", "000", LOCATION_COUNTER, "000", "000000000000"));
			currentline.addError(111);
			LOCATION_COUNTER++;
        	valid_reset = false;
        }
        else {
        	//ERROR: DIRECTIVE DOES NOT HAVE REQUIRED LABEL
			currentline = (new Line(linecounter, "", "SWR","000", "000", LOCATION_COUNTER, "000", "000000000000"));
			currentline.addError(111);
			LOCATION_COUNTER++;
        	valid_reset = false;
        }

        // Check for operands
        if (tokens.length > 2 && valid_reset) {
        	// Check for invalid expression
        	if (tokens[2].contains("+")) {
        		// ERROR: USE OF EXPRESSIONS IS NOT ALLOWED IN THIS DIRECTIVE
    			currentline = (new Line(linecounter, "", "SWR","000", "000", LOCATION_COUNTER, "000", "000000000000"));
    			currentline.addError(625);
    			LOCATION_COUNTER++;
        	}
        	else if (tokens[2].contains("=")) {
        		// ERROR: USE OF LITERALS IS NOT ALLOWED
    			currentline = (new Line(linecounter, "", "SWR","000", "000", LOCATION_COUNTER, "000", "000000000000"));
    			currentline.addError(608);
    			LOCATION_COUNTER++;
        	}
        	else if (tokens[2].contains("(") && tokens[2].contains(")")) {
        		// ERROR: CANT USE INDEXED MEMORY REFERENCES
    			currentline = (new Line(linecounter, "", "SWR","000", "000", LOCATION_COUNTER, "000", "000000000000"));
    			currentline.addError(616);
    			LOCATION_COUNTER++;
        	}
        	// Check for invalid characters
        	else if (tokens[2].contains("!") || tokens[2].contains("?") || tokens[2].contains("#") || 
    				tokens[2].contains("_") || tokens[2].contains("/") || tokens[2].contains("\"") || 
    				tokens[2].contains(".") || tokens[2].contains("@") || tokens[2].contains("$") ||
    				tokens[2].contains("%") || tokens[2].contains("^") || tokens[2].contains("&") ||
    				tokens[2].contains("[") || tokens[2].contains("]") || tokens[2].contains("{") ||
    				tokens[2].contains("}") || tokens[2].contains("<") || tokens[2].contains(">") || 
    				tokens[2].contains("(") || tokens[2].contains(")")){
				// ERROR: ADDRESS CONSTANT CONTAINS INVALID CHARACTERS 
    			currentline = (new Line(linecounter, "", "SWR","000", "000", LOCATION_COUNTER, "000", "000000000000"));
    			currentline.addError(621); 
    			LOCATION_COUNTER++;

			}
        	else if (tokens[2].contains(",")) {
        		// Make sure there are not commas at the beginning or end
        		if (tokens[2].charAt(0) != ',' && tokens[2].charAt(tokens[2].length()-1) != ',') {

        			// Make sure the length is atleast 2
        			if (tokens[2].length() > 2) {
        				
        				// Check to make sure there aren't too many commas
        				if (tokens[2].split(",").length < 3) {
        					String page = tokens[2].split(",")[0];
        					String loc = tokens[2].split(",")[1];

        					if (isNumber(page)) {
        						// Check memory page range if it's numeric
        						if (!(Double.valueOf(page) >= 0) || !(Double.valueOf(page) <= 3)) {

        							//ERROR: THIS CALL TO RESET USES AN INVALID MEMORY PAGE NUMBER
        							currentline = (new Line(linecounter, "", "SWR","000", "000", LOCATION_COUNTER, "000", "000000000000"));
        							currentline.addError(121);
        							LOCATION_COUNTER++;
        							valid_reset = false;
        						}
        					}
        					else if (page.charAt(0) == '-') {
        						//ERROR: THIS CALL TO RESET USES AN INVALID MEMORY PAGE NUMBER
    							currentline = (new Line(linecounter, "", "SWR","000", "000", LOCATION_COUNTER, "000", "000000000000"));
    							currentline.addError(121);
    							LOCATION_COUNTER++;
    							valid_reset = false;
        					}
        					else if (page.contains("*")) {
        						// ERROR: PAGE NUMBER CANT USE STAR NOTATION
        						//ERROR: THIS CALL TO RESET USES AN INVALID MEMORY PAGE NUMBER
    							currentline = (new Line(linecounter, "", "SWR","000", "000", LOCATION_COUNTER, "000", "000000000000"));
    							currentline.addError(628);
    							LOCATION_COUNTER++;
    							valid_reset = false;
        					}
        					else {
        						if (SYMBOL_TABLE.symbolIsDefined(page)) {
        							int page_value = Integer.valueOf(SYMBOL_TABLE.getString(page));
        							if (!(page_value >= 0) || !(page_value <= 3)) {
        								//ERROR: THIS CALL TO RESET USES AN INVALID MEMORY PAGE NUMBER
            							currentline = (new Line(linecounter, "", "SWR","000", "000", LOCATION_COUNTER, "000", "000000000000"));
            							currentline.addError(121);
            							LOCATION_COUNTER++;
            							valid_reset = false;
        							}
        						}
        						else {
        							// ERROR: SYMBOL MUST BE PREVIOUSLY DEFINED
        							currentline = (new Line(linecounter, "", "SWR","000", "000", LOCATION_COUNTER, "000", "000000000000"));
        							currentline.addError(304);
        							LOCATION_COUNTER++;
        							valid_reset = false;
        						}
        					}
        					// Check location value
        					if (valid_reset && isNumber(loc)) {
        						if (!(Double.valueOf(loc) >= 0) || !(Double.valueOf(loc) <= 4095) && valid_reset) {

        							//ERROR: THIS CALL TO RESET USES A RESET LOCATION OUT OF BOUNDS
        							currentline = (new Line(linecounter, "", "SWR","000", "000", LOCATION_COUNTER, "000", "000000000000"));
        							currentline.addError(121);
        							LOCATION_COUNTER++;
        							valid_reset = false;
        						}
        						else if (valid_reset) {
        							reset_location = Integer.valueOf(loc);
        						}
        					}
        					else if (valid_reset && loc.charAt(0) == '-') {
        						//ERROR: THIS CALL TO RESET USES A RESET LOCATION OUT OF BOUNDS
    							currentline = (new Line(linecounter, "", "SWR","000", "000", LOCATION_COUNTER, "000", "000000000000"));
    							currentline.addError(121);
    							LOCATION_COUNTER++;
    							valid_reset = false;
        					}
        					// Check label and value
        					else if (valid_reset && !isNumber(loc)) {

        						// Get reset location from symbol table if defined
        						if (SYMBOL_TABLE.symbolIsDefined(loc)) {
        							reset_location = SYMBOL_TABLE.getLocation(loc);
        						}
        						else {
        							// ERROR: SYMBOL MUST BE PREVIOUSLY EQUATED BEFORE USED AS AN OPERAND
        							currentline = (new Line(linecounter, "", "SWR","000", "000", LOCATION_COUNTER, "000", "000000000000"));
        							currentline.addError(304);
        							LOCATION_COUNTER++;
        							valid_reset = false;
        						}  
        						if (loc.charAt(0) == '*') {
        							
        							if (loc.length() == 1) {
        								// VALID ONE STAR
    	        						currentline = (new Line(linecounter, tokens[0], "RESET", page, loc, LOCATION_COUNTER, "000", "000000000000"));
    	        						updateSymbolTable(tokens);
    	        						valid_reset = false;
        							}
        							else if (loc.substring(1).contains("*")) {
        								// ERROR: ONLY ONE STAR IN STAR NOTATION
            							currentline = (new Line(linecounter, "", "SWR","000", "000", LOCATION_COUNTER, "000", "000000000000"));
            							currentline.addError(289);
            							LOCATION_COUNTER++;
            							valid_reset = false;
        							}
        							else if (loc.charAt(1) != '+' && loc.charAt(1) != '-') {
        								// ERROR: OPERATOR MUST FOLLOW * NOTATION
            							currentline = (new Line(linecounter, "", "SWR","000", "000", LOCATION_COUNTER, "000", "000000000000"));
            							currentline.addError(306);
            							LOCATION_COUNTER++;
            							valid_reset = false;
        								// line
        							}
        							else if (isNumber(loc.substring(2))) {
        								int result = 0;
        								if (loc.charAt(1) == '-') {
        									result = LOCATION_COUNTER - Integer.valueOf(loc.substring(2));
        								}
        								else {
        									result = LOCATION_COUNTER + Integer.valueOf(loc.substring(2));
        								}
        								// Check if result is in range and print output
        								if (result >= 0 && result <= 4095) {
        									LOCATION_COUNTER = result;
        	        						currentline = (new Line(linecounter, tokens[0], "RESET", page, loc, LOCATION_COUNTER, "000", "000000000000"));
        	        						updateSymbolTable(tokens);
        	        						valid_reset = false;
        								}
        								else {
        									//ERROR: RESET VALUE OUT OF RANGE
        									currentline = (new Line(linecounter, "", "SWR","000", "000", LOCATION_COUNTER, "000", "000000000000"));
        									currentline.addError(121);
        									LOCATION_COUNTER++;
        									valid_reset = false;
        									
        								}
        							}
        							else if (!SYMBOL_TABLE.symbolIsDefined(loc.substring(2))) {
        								// ERROR: SYMBOL MUST BE PREVIOUSLY DEFINED IN ORDER TO BE USED IN STAR NOTATION
        							}
        							else if (SYMBOL_TABLE.symbolIsDefined(loc.substring(2))) {
        								int result = 0;
        								if (loc.charAt(1) == '-') {
        									result = LOCATION_COUNTER - Integer.valueOf(SYMBOL_TABLE.getString(loc.substring(2)));
        								}
        								else {
        									result = LOCATION_COUNTER + Integer.valueOf(SYMBOL_TABLE.getString(loc.substring(2)));
        								}

        								// Check reset value and print output
        								if (result >= 0 && result <= 4095) {
        									LOCATION_COUNTER = result;
        	        						currentline = (new Line(linecounter, tokens[0], "RESET", page, loc, LOCATION_COUNTER, "000", "000000000000"));
        	        						updateSymbolTable(tokens);
        	        						valid_reset = false;
        								}
        								else {
        									//ERROR: RESET VALUE OUT OF RANGE
        									currentline = (new Line(linecounter, "", "SWR","000", "000", LOCATION_COUNTER, "000", "000000000000"));
        									currentline.addError(121);
        									LOCATION_COUNTER++;
        									valid_reset = false;
        								}        							
        							}
        						}
        					}


        					// Print breakdown for valid reset call
        					if (valid_reset) {
        						LOCATION_COUNTER = reset_location;
        						currentline = (new Line(linecounter, tokens[0], "RESET", page, loc, LOCATION_COUNTER, "000", "000000000000"));
        						updateSymbolTable(tokens);
        					}
        				}
        				else {
        					// ERROR: TOO MANY COMMAS
							currentline = (new Line(linecounter, "", "SWR","000", "000", LOCATION_COUNTER, "000", "000000000000"));
							currentline.addError(606);
							LOCATION_COUNTER++;
        				}
        			}
        			else {
        				// ERROR: RESET MUST HAVE BOTH PAGE AND MEMORY LOCATION
        				currentline = (new Line(linecounter, "", "SWR","000", "000", LOCATION_COUNTER, "000", "000000000000"));
        				currentline.addError(102);
        				LOCATION_COUNTER++;
        			}
        		}
        		else {
        			// ERROR: OPERAND CANNOT HAVE A COMMA AT THE BEGINNING OR END OF OPERAND FIELD
    				currentline = (new Line(linecounter, "", "SWR","000", "000", LOCATION_COUNTER, "000", "000000000000"));
    				currentline.addError(624);
    				LOCATION_COUNTER++;
        		}

        	}
        	else {
        		//ERROR: INVALID OPERAND SYNTAX, BOTH MEMORY PAGE AND LOCATION MUST BE IN OPERAND
				currentline = (new Line(linecounter, "", "SWR","000", "000", LOCATION_COUNTER, "000", "000000000000"));
				currentline.addError(140);
				LOCATION_COUNTER++;
        	}
        }
        else if (valid_reset){
        	// ERROR: DIRECTIVE USED CONTAINS NO REQUIRED OPERANDS
			currentline = (new Line(linecounter, "", "SWR","000", "000", LOCATION_COUNTER, "000", "000000000000"));
			currentline.addError(010);
			LOCATION_COUNTER++;
        }
    }
 
    private static void parseAndCheckEQU (String[] tokens) {
    	Boolean valid_EQU = true;
        // Check if required label is present        
        if (!tokens[0].equals("")) {
            
            // Check label for correct formatting
            if (!isValidLabel(tokens[0])) {
				currentline = (new Line(linecounter, "", "SWR","000", "000", LOCATION_COUNTER, "000", "000000000000"));
				LOCATION_COUNTER++;
				checkForValidLabel(tokens[0]);
                valid_EQU = false;
            }
        }
        else if (tokens.length > 2){
            //ERROR: DIRECTIVE DOES NOT HAVE REQUIRED LABEL
			currentline = (new Line(linecounter, "", "SWR","000", "000", LOCATION_COUNTER, "000", "000000000000"));
			currentline.addError(112);
			LOCATION_COUNTER++;
        	valid_EQU = false;
        }
        else {
        	//ERROR: DIRECTIVE DOES NOT HAVE REQUIRED LABEL
			currentline = (new Line(linecounter, "", "SWR","000", "000", LOCATION_COUNTER, "000", "000000000000"));
			currentline.addError(112);
			LOCATION_COUNTER++;
        	valid_EQU = false;
        }

        if (tokens.length > 2 && valid_EQU) {
        	// Check operand for correct format and bounds

        	if (isNumber(tokens[2])) {
        		
        		// Check if numeric memory reference is in range
        		if (!(Double.valueOf(tokens[2]) >= 0) || !(Double.valueOf(tokens[2]) <= 4095)) {
        			
        			//ERROR: DIRECTIVE NUMERIC OUT OF RANGE
    				currentline = (new Line(linecounter, "", "SWR","000", "000", LOCATION_COUNTER, "000", "000000000000"));
    				currentline.addError(115);      			
    				LOCATION_COUNTER++;
        		}
        		else {
    				currentline = (new Line(linecounter, tokens[0], "EQU",tokens[2], LOCATION_COUNTER, "000", "000000000000"));
    				updateSymbolTable(tokens);
        		}
        	}

        	else if (tokens[2].contains(",")) {
        		//ERROR: NO COMMAS
				currentline = (new Line(linecounter, "", "SWR","000", "000", LOCATION_COUNTER, "000", "000000000000"));
				currentline.addError(116);   
				LOCATION_COUNTER++;
        	}
        	else if (isLiteral(tokens[2])) {
        		// ERROR: CANNOT EQUATE TO A LITERAL VALUE
    			currentline = (new Line(linecounter, "", "SWR","000", "000", LOCATION_COUNTER, "000", "000000000000"));
    			currentline.addError(293);
    			LOCATION_COUNTER++;
        	}
        	else if (tokens[2].charAt(0) == '-' && isNumber(tokens[2].substring(1))) {
        		// ERROR: EQUATED VALUE OUT OF RANGE
				currentline = (new Line(linecounter, "", "SWR","000", "000", LOCATION_COUNTER, "000", "000000000000"));
				currentline.addError(115);           		
				LOCATION_COUNTER++;
    		}
        	else if ((tokens[2].contains("+") && tokens[2].charAt(0) != '*')) {
				currentline = (new Line(linecounter, "", "SWR","000", "000", LOCATION_COUNTER, "000", "000000000000"));
				currentline.addError(625);   
				LOCATION_COUNTER++;
        	}
        	else if (tokens[2].contains("(") && tokens[2].contains(")")) {
				currentline = (new Line(linecounter, "", "SWR","000", "000", LOCATION_COUNTER, "000", "000000000000"));
				currentline.addError(616);  
				LOCATION_COUNTER++;
        	}
        	else if (tokens[2].contains("!") || tokens[2].contains("?") || tokens[2].contains("#") || 
    				tokens[2].contains("_") || tokens[2].contains("/") || tokens[2].contains("\"") || 
    				tokens[2].contains(".") || tokens[2].contains("@") || tokens[2].contains("$") ||
    				tokens[2].contains("%") || tokens[2].contains("^") || tokens[2].contains("&") ||
    				tokens[2].contains("[") || tokens[2].contains("]") || tokens[2].contains("{") ||
    				tokens[2].contains("}") || tokens[2].contains("<") || tokens[2].contains(">") ||
    				tokens[2].contains("(") || tokens[2].contains(")") || tokens[2].contains("~")){
				// ERROR: ADDRESS CONSTANT CONTAINS INVALID CHARACTERS 
    			currentline = (new Line(linecounter, "", "SWR","000", "000", LOCATION_COUNTER, "000", "000000000000"));
    			currentline.addError(621); 
    			LOCATION_COUNTER++;

			}
        	// Handle star notation
        	else if (tokens[2].charAt(0) == '*') {
        		if (tokens[2].length() > 1) {
        			// Check that next character is an operator
        			if (tokens[2].charAt(1) == '+' || tokens[2].charAt(1) == '-') {
        				String operand = tokens[2].substring(2);
        				
        				if (operand.contains("*")) {
        					// ERROR: ONLY ONE STAR ALLOWED IN STAR NOTATION
							currentline = (new Line(linecounter, "", "SWR","000", "000", LOCATION_COUNTER, "000", "000000000000"));
							currentline.addError(301);   
							LOCATION_COUNTER++;
        				}
        				else if (operand.contains("+") || operand.contains("-")) {
        					// ERROR: EXPRESSIONS AREN't ALLOWED IN THIS OPERATION
							currentline = (new Line(linecounter, "", "SWR","000", "000", LOCATION_COUNTER, "000", "000000000000"));
							currentline.addError(625);   
							LOCATION_COUNTER++;
        				}
        				else {
        					if (isNumber(operand)) {
        						if (!(Double.valueOf(operand) >= 0) || !(Double.valueOf(operand) <= 4095)) {
        							// ERROR: EQUATED VALUE OUT OF RANGE
        							currentline = (new Line(linecounter, "", "SWR","000", "000", LOCATION_COUNTER, "000", "000000000000"));
        							currentline.addError(115);    
        							LOCATION_COUNTER++;
        						}
        						else {
        							int result = 0;
        							if (tokens[2].charAt(1) == '+') {
        								result = LOCATION_COUNTER + Integer.valueOf(operand);
        							}
        							else if (tokens[2].charAt(1) == '-') {
        								result = LOCATION_COUNTER - Integer.valueOf(operand);
        							}

        							// Check if result is valid
        							if (result >= 0 && result <= 4095) {
        								currentline = (new Line(linecounter, tokens[0], "EQU",tokens[2], LOCATION_COUNTER, "000", "000000000000"));
        								updateSymbolTable(tokens);
        							}
        							else {
        								// ERROR: EQUATED VALUE OUT OF RANGE
        								currentline = (new Line(linecounter, "", "SWR","000", "000", LOCATION_COUNTER, "000", "000000000000"));
        								currentline.addError(115);   
        								LOCATION_COUNTER++;
        							}
        						}
        					}
        					else if (!SYMBOL_TABLE.symbolIsDefined(operand)) {
        						// ERROR: symbol must be previously equated before use as an operand
        						currentline = (new Line(linecounter, "", "SWR","000", "000", LOCATION_COUNTER, "000", "000000000000"));
        						currentline.addError(305);
        						LOCATION_COUNTER++;
        					}
        					else {
        						// VALID: STAR WITH +/- ELABEL
        						int result = 0;
        						if (tokens[2].charAt(1) == '+') {
        							result = LOCATION_COUNTER + Integer.valueOf(SYMBOL_TABLE.getString(operand));
        						}
        						else if (tokens[2].charAt(1) == '-') {
        							result = LOCATION_COUNTER - Integer.valueOf(SYMBOL_TABLE.getString(operand));
        						}

        						// Check if result is valid
        						if (result >= 0 && result <= 4095) {
        							currentline = (new Line(linecounter, tokens[0], "EQU",tokens[2], LOCATION_COUNTER, "000", "000000000000"));
        							updateSymbolTable(tokens);
        						}
        						else {
        							// ERROR: EQUATED VALUE OUT OF RANGE
        							currentline = (new Line(linecounter, "", "SWR","000", "000", LOCATION_COUNTER, "000", "000000000000"));
        							currentline.addError(115);   
        							LOCATION_COUNTER++;
        						}
        					}
        				}
        			}
        			else {
        				// ERROR: OPERATOR MUST FOLLOW STAR
	    				currentline = (new Line(linecounter, "", "SWR","000", "000", LOCATION_COUNTER, "000", "000000000000"));
	    				currentline.addError(306);   
	    				LOCATION_COUNTER++;
        			}
        		}
        		else {
        			// VALID: ONLY STAR
    				currentline = (new Line(linecounter, tokens[0], "EQU",tokens[2], LOCATION_COUNTER, "000", "000000000000"));
    				updateSymbolTable(tokens);
        		}
        	
        	}
        	else if (tokens[2].contains("*")) {
    			currentline = (new Line(linecounter, "", "SWR","000", "000", LOCATION_COUNTER, "000", "000000000000"));
    			currentline.addError(301);
    			LOCATION_COUNTER++;
        	}
        	// Handle literal

        	else if (tokens[2].contains("++") || tokens[2].contains("--") || tokens[2].contains("+-") || tokens[2].contains("-+")) {
    			currentline = (new Line(linecounter, "", "SWR","000", "000", LOCATION_COUNTER, "000", "000000000000"));
    			currentline.addError(311);
    			LOCATION_COUNTER++;
        	}
        	else if (tokens[2].charAt(0) == '+') {
        		if (tokens[2].length() > 1){
        			int number = Integer.valueOf(tokens[2].substring(1));
        			if (number >= 0 && number <= 4095) {
        				currentline = (new Line(linecounter, tokens[0], "EQU",tokens[2], LOCATION_COUNTER, "000", "000000000000"));
        				updateSymbolTable(tokens);
        			}
        			else {
        				// ERROR: EQUATED VALUE OUT OF RANGE
        				currentline = (new Line(linecounter, "", "SWR","000", "000", LOCATION_COUNTER, "000", "000000000000"));
        				currentline.addError(115);   
        				LOCATION_COUNTER++;
        			}
        		}
        		else {
        			// ERROR: OPERATORS CANNOT BE THE FIRST OR LAST CHARACTERS OF AN EXPRESSION
        			currentline = (new Line(linecounter, "", "SWR","000", "000", LOCATION_COUNTER, "000", "000000000000"));
        			currentline.addError(627); 
        			LOCATION_COUNTER++;
        		}
        	}
        	else if (tokens[2].charAt(0) == '-') {
        		// ERROR: EQUATED VALUE OUT OF RANGE
				currentline = (new Line(linecounter, "", "SWR","000", "000", LOCATION_COUNTER, "000", "000000000000"));
				currentline.addError(115);   
				LOCATION_COUNTER++;
        	}
        	
        	// Handle label operand
        	else if (SYMBOL_TABLE.symbolIsDefined(tokens[2])) {
    				currentline = (new Line(linecounter, tokens[0], "EQU",tokens[2], LOCATION_COUNTER, "000", "000000000000"));
        			updateSymbolTable(tokens);
        	}
        	else {
        		// ERROR: symbol must be previously equated before use as an operand
				currentline = (new Line(linecounter, "", "SWR","000", "000", LOCATION_COUNTER, "000", "000000000000"));
				currentline.addError(305);
				LOCATION_COUNTER++;
        	}
        }
        else if (valid_EQU) {
    		// ERROR: DIRECTIVE USED CONTAINS NO REQUIRED OPERANDS
			currentline = (new Line(linecounter, "", "SWR","000", "000", LOCATION_COUNTER, "000", "000000000000"));
			currentline.addError(010);
			LOCATION_COUNTER++;
    	}
    }
    
    private static void parseAndCheckEQUe (String[] tokens) {
       	Boolean valid_EQUe = true;
        // Check if required label is present        
        if (!tokens[0].equals("")) {
            
            // Check label for correct formatting
            if (!isValidLabel(tokens[0])) {
				currentline = (new Line(linecounter, "", "SWR","000", "000", LOCATION_COUNTER, "000", "000000000000"));
				LOCATION_COUNTER++;
				
				// Print label errors
                checkForValidLabel(tokens[0]);
                valid_EQUe = false;
            }
        }
        else if (tokens.length > 2){
            //ERROR: DIRECTIVE DOES NOT HAVE REQUIRED LABEL
			currentline = (new Line(linecounter, "", "SWR","000", "000", LOCATION_COUNTER, "000", "000000000000"));
			currentline.addError(112);
			LOCATION_COUNTER++;
        	valid_EQUe = false;
        }
        else {
        	//ERROR: DIRECTIVE DOES NOT HAVE REQUIRED LABEL
			currentline = (new Line(linecounter, "", "SWR","000", "000", LOCATION_COUNTER, "000", "000000000000"));
			currentline.addError(112);
			LOCATION_COUNTER++;
        	valid_EQUe = false;
        }
    	
    	// Make sure there are operands
    	if (tokens.length > 2 && valid_EQUe) {
    		if (tokens[2].contains(",")) {
    			// ERROR: EXPRESSION CANNOT CONTAIN COMMAS
				currentline = (new Line(linecounter, "", "SWR","000", "000", LOCATION_COUNTER, "000", "000000000000"));
				currentline.addError(404);
				LOCATION_COUNTER++;
    		}
    		
    		else if (tokens[2].contains("(")) {
    			// ERROR: DIRECTIVES ARE NOT ALLOWED TO USE MEMORY REFERENCES WITH AN INDEX: LINE WILL BE REPLACED BY A NOP
				currentline = (new Line(linecounter, "", "SWR","000", "000", LOCATION_COUNTER, "000", "000000000000"));
				currentline.addError(616);
				LOCATION_COUNTER++;
    		}
    		else if (tokens[2].contains("=")) {
    			// ERROR: LITERALS ARE NOT VALID OPERANDS IN EQUe
				currentline = (new Line(linecounter, "", "SWR","000", "000", LOCATION_COUNTER, "000", "000000000000"));
				currentline.addError(617);
				LOCATION_COUNTER++;
    			
    		}
    		else if (tokens[2].contains("!") || tokens[2].contains("?") || tokens[2].contains("#") || 
    				tokens[2].contains("_") || tokens[2].contains("/") || tokens[2].contains("\"") || 
    				tokens[2].contains(".") || tokens[2].contains("@") || tokens[2].contains("$") ||
    				tokens[2].contains("%") || tokens[2].contains("^") || tokens[2].contains("&") ||
    				tokens[2].contains("[") || tokens[2].contains("]") || tokens[2].contains("{") ||
    				tokens[2].contains("}") || tokens[2].contains("<") || tokens[2].contains(">")) {
				// ERROR: ADDRESS CONSTANT CONTAINS INVALID CHARACTERS 
    			currentline = (new Line(linecounter, "", "SWR","000", "000", LOCATION_COUNTER, "000", "000000000000"));
    			currentline.addError(621); 
    			LOCATION_COUNTER++;

			}
    		else if (tokens[2].charAt(tokens[2].length()-1) == '+' || tokens[2].charAt(tokens[2].length()-1) == '-' ) {
    			// ERROR: OPERATORS CANNOT BE THE FIRST OR LAST CHARACTERS OF AN EXPRESSION
    			currentline = (new Line(linecounter, "", "SWR","000", "000", LOCATION_COUNTER, "000", "000000000000"));
    			currentline.addError(627); 
    			LOCATION_COUNTER++;
    		}
    		else if (tokens[2].charAt(0) == '*') {
    			String expression = tokens[2];

    			// Check for invalid multiple stars
    			if (expression.substring(1).contains("*")) {
    				// ERROR: 283
    				currentline = (new Line(linecounter, "", "SWR","000", "000", LOCATION_COUNTER, "000", "000000000000"));
    				currentline.addError(283);
    				LOCATION_COUNTER++;
    			}
    			else {

    				if (tokens[2].length() > 1) {
    					// Check to make sure an operator follows *
    					if (expression.charAt(1) == '+' || expression.charAt(1) == '-') {
    						String [] operands = getExpressionOperands(expression);

    						// Check to make sure no more than 4 operators are present
    						// ** length of 5 used for the condition since 0+ is added before 

    						if (operands.length > 5) {
    							// ERROR: ONLY 4 OPERATORS ALLOWED IN AN EQU-EXPRESSION
    							currentline = (new Line(linecounter, "", "SWR","000", "000", LOCATION_COUNTER, "000", "000000000000"));    							
    							currentline.addError(300);
    							LOCATION_COUNTER++;
    						}
    						// Check for back to back operators
    						else if (expression.contains("++") || expression.contains("--") || expression.contains("+-") || expression.contains("-+")) {
    							// ERROR: INVALID EXPRESSION, CANNOT HAVE CONSECUTIVE OPERATORS IN EXPRESSION
    							currentline = (new Line(linecounter, "", "SWR","000", "000", LOCATION_COUNTER, "000", "000000000000"));    							
    							currentline.addError(402);
    							LOCATION_COUNTER++;
    						}
    						// Check for operator as last character of expression
    						else if (expression.charAt(expression.length()-1) == '+' || expression.charAt(expression.length()-1) == '-') {
    							// ERROR: INVALID EXPRESSION, CANT HAVE LAST VALUE BE OPERATOR
    							currentline = (new Line(linecounter, "", "SWR","000", "000", LOCATION_COUNTER, "000", "000000000000"));    							
    							currentline.addError(403);
    							LOCATION_COUNTER++;
    						}
    						else {

    							// Check to make sure operands are valid before attempting to evaluate
    							if (areValidEquExpressionOperands(operands)) {
    								int expression_result = evaluateEQUExpression(expression);


    								// Make sure result is in valid memory range            		
    								if (!(expression_result >=0) || !(expression_result <= 4095)) {

    									// ERROR: RESULT OF EXPRESSION IS NOT WITHIN MEMORY RANGE
    									currentline = (new Line(linecounter, "", "SWR","000", "000", LOCATION_COUNTER, "000", "000000000000"));
    									currentline.addError(299);
    									LOCATION_COUNTER++;
    								}

    								// VALID LINE
    								else {
    									currentline = (new Line(linecounter, tokens[0], "EQUe", tokens[2], LOCATION_COUNTER, "000", "000000000000"));
    									updateSymbolTable(tokens);
    								}
    							}
    							else {
    								//ERROR: Operands of this EQU-expression are invalid
    								currentline = (new Line(linecounter, "", "SWR","000", "000", LOCATION_COUNTER, "000", "000000000000"));
    								currentline.addError(626);
    								LOCATION_COUNTER++;
    								checkEquExpressionOperandSyntax(operands);
    							}
    						}
    					}

    					else {
    						// ERROR: AN OPERATOR MUST FOLLOW * IN AN EQU-expression
    						currentline = (new Line(linecounter, "", "SWR","000", "000", LOCATION_COUNTER, "000", "000000000000"));
    						currentline.addError(306);
    						LOCATION_COUNTER++;
    					}
    				}
    				else {
    					// VALID: SINGLE STAR AS OPERAND
						currentline = (new Line(linecounter, tokens[0], "EQUe", tokens[2], LOCATION_COUNTER, "000", "000000000000"));
    					updateSymbolTable(tokens);
    				}
    			}
    		}
    		else if (tokens[2].contains("*") && tokens[2].charAt(0) != '*') {
    			// ERROR: 
				currentline = (new Line(linecounter, "", "SWR","000", "000", LOCATION_COUNTER, "000", "000000000000"));
				currentline.addError(284);
				LOCATION_COUNTER++;
    		}
    		else {
    			String [] operands = getExpressionOperands(tokens[2]);
    			
    			if (operands.length > 5) {
					// ERROR: ONLY 4 OPERATORS ALLOWED IN AN EQU-EXPRESSION
    				currentline = (new Line(linecounter, "", "SWR","000", "000", LOCATION_COUNTER, "000", "000000000000"));
    				currentline.addError(300);
    				LOCATION_COUNTER++;
				}
    			
    			else if (areValidEquExpressionOperands(operands)) {
					int expression_result = evaluateEQUExpression(tokens[2]);

					// Make sure result is in valid memory range            		
					if (!(expression_result >=0) || !(expression_result <= 4095)) {
						
						// ERROR: RESULT OF EXPRESSION IS NOT WITHIN MEMORY RANGE
						currentline = (new Line(linecounter, "", "SWR","000", "000", LOCATION_COUNTER, "000", "000000000000"));
						currentline.addError(299);
						LOCATION_COUNTER++;
					}
					else {
						currentline = (new Line(linecounter, tokens[0], "EQUe", tokens[2], LOCATION_COUNTER, "000", "000000000000"));
						updateSymbolTable(tokens);
					}
				}
				else {
					//ERROR: Operands of this EQU-expression are invalid
					currentline = (new Line(linecounter, "", "SWR","000", "000", LOCATION_COUNTER, "000", "000000000000"));
					currentline.addError(626);
					LOCATION_COUNTER++;
					checkEquExpressionOperandSyntax(operands);
				}
    		}
    		
    	}
    	else if (valid_EQUe){
    		// ERROR: DIRECTIVE USED CONTAINS NO REQUIRED OPERANDS
			currentline = (new Line(linecounter, "", "SWR","000", "000", LOCATION_COUNTER, "000", "000000000000"));
			currentline.addError(010);
			LOCATION_COUNTER++;
    	}     	
    }
    
    private static void parseAndCheckENT (String[] tokens) {
    	Boolean invalid_label = false;

    	if (!tokens[0].equals("")) {
    		invalid_label = true;
    		//ERROR: UNABLE TO ASSIGN LABEL TO DIRECTIVES EXT OR ENT
    	}
    	if (tokens.length >2) {
    		if (tokens[2].charAt(0) == ',' || tokens[2].charAt(tokens[2].length()-1) == ',') {
    			// ERROR: INVALID OPERAND FIELD, COMMAS CANNOT BE THE FIRST CHARACTER 
				currentline = (new Line(linecounter, "", "SWR","000", "000", LOCATION_COUNTER, "000", "000000000000"));
				currentline.addError(624);
				LOCATION_COUNTER++;
    			
    		}
    		else if (tokens[2].contains("*")) {
    			// ERROR: STAR NOTATION IS NOT ALLOWED IN THIS DIRECTIVE
				currentline = (new Line(linecounter, "", "SWR","000", "000", LOCATION_COUNTER, "000", "000000000000"));
				currentline.addError(622);
				LOCATION_COUNTER++;
    		}
    		else if (tokens[2].contains("(") && tokens[2].contains(")")) {
    			// ERROR: CANT HAVE INDEXED MEMORY REFS
				currentline = (new Line(linecounter, "", "SWR","000", "000", LOCATION_COUNTER, "000", "000000000000"));
				currentline.addError(616);
				LOCATION_COUNTER++;
    		}
    		else if ((tokens[2].contains("+") || tokens[2].contains("-") && tokens[2].charAt(0) != '=')) {
    			// ERROR: EXPRESSIONS ARE NOT ALLOWED TO BE USED AS AN OPERAND IN THIS INSTRUCTION
				currentline = (new Line(linecounter, "", "SWR","000", "000", LOCATION_COUNTER, "000", "000000000000"));
				currentline.addError(625);
				LOCATION_COUNTER++;
    		}
    		else if (tokens[2].contains("=")) {
    			// ERROR: LITERALS ARE NOT VALID OPERANDS FOR THIS DIRECTIVE
				currentline = (new Line(linecounter, "", "SWR","000", "000", LOCATION_COUNTER, "000", "000000000000"));
				currentline.addError(608);
				LOCATION_COUNTER++;
    		}

    		else if (tokens[2].contains("!") || tokens[2].contains("?") || tokens[2].contains("#") || 
    				tokens[2].contains("_") || tokens[2].contains("/") || tokens[2].contains("\"") || 
    				tokens[2].contains(".") || tokens[2].contains("@") || tokens[2].contains("$") ||
    				tokens[2].contains("%") || tokens[2].contains("^") || tokens[2].contains("&") ||
    				tokens[2].contains("[") || tokens[2].contains("]") || tokens[2].contains("{") ||
    				tokens[2].contains("}") || tokens[2].contains("<") || tokens[2].contains(">") ||
    				tokens[2].contains("(") || tokens[2].contains(")")){
				// ERROR: ENT CONTAINS INVALID CHARACTERS 
    			currentline = (new Line(linecounter, "", "SWR","000", "000", LOCATION_COUNTER, "000", "000000000000"));
    			currentline.addError(621); 
    			LOCATION_COUNTER++;
    		}
    		else {
    			String[] rlabels = tokens[2].split(",");
    			int rlabel_count = rlabels.length;
    			if (rlabels.length > 4) {

    				//ERROR: ONLY FOUR LABELS ARE ALLOWED TO BE DECLARED IN DIRECTIVES EXT OR ENT
    				currentline = (new Line(linecounter, "", "SWR","000", "000", LOCATION_COUNTER, "000", "000000000000"));
    				LOCATION_COUNTER++;

    				// Print error for a present label that't not allowed
    				if (invalid_label) {
    					currentline.addError(160);
    				}
    				currentline.addError(161);
    			}
    			else {
    				updateSymbolTable(tokens);
    				if (rlabel_count == 1) {
    					currentline = (new Line(linecounter, "", "ENT", rlabels[0], LOCATION_COUNTER, "000", "000000000000"));
    				}
    				else if (rlabel_count == 2) {
    					currentline = (new Line(linecounter, "", "ENT",rlabels[0], rlabels[1], LOCATION_COUNTER, "000", "000000000000"));
    				}
    				else if (rlabel_count == 3) {
    					currentline = (new Line(linecounter, "", "ENT", rlabels[0], rlabels[1], rlabels[2], LOCATION_COUNTER, "000", "000000000000"));
    				}
    				else if (rlabel_count == 4) {
    					currentline = (new Line(linecounter, "", "ENT", rlabels[0], rlabels[1], rlabels[2], rlabels[3], LOCATION_COUNTER, "000", "000000000000"));
    				}

    				int i = 0;
    				while (i < rlabels.length) {
    					checkForValidLabel(rlabels[i]);
    					i++;
    				}
    			}
    		}
    	}
    	else {
    		// ERROR: DIRECTIVE USED CONTAINS NO REQUIRED OPERANDS
    		currentline = (new Line(linecounter, "", "SWR","000", "000", LOCATION_COUNTER, "000", "000000000000"));
    		currentline.addError(010);
    		LOCATION_COUNTER++;
    	}

    }
    
    private static void parseAndCheckEXT (String[] tokens) {
    	Boolean invalid_label = false;
    	
    	if (!tokens[0].equals("")) {
    		invalid_label = true;
    		//ERROR: UNABLE TO ASSIGN LABEL TO DIRECTIVES EXT OR ENT
    	}
    	if (tokens.length >2) {
    		if (tokens[2].charAt(0) == ',' || tokens[2].charAt(tokens[2].length()-1) == ',') {
    			// ERROR: INVALID OPERAND FIELD, COMMAS CANNOT BE THE FIRST CHARACTER 
				currentline = (new Line(linecounter, "", "SWR","000", "000", LOCATION_COUNTER, "000", "000000000000"));
				currentline.addError(624);
				LOCATION_COUNTER++;
    			
    		}
    		else if (tokens[2].contains("*")) {
    			// ERROR: STAR NOTATION IS NOT ALLOWED IN THIS DIRECTIVE
				currentline = (new Line(linecounter, "", "SWR","000", "000", LOCATION_COUNTER, "000", "000000000000"));
				currentline.addError(622);
				LOCATION_COUNTER++;
    		}
       		else if (tokens[2].contains("=")) {
    			// ERROR: LITERALS ARE NOT VALID OPERANDS FOR THIS DIRECTIVE
				currentline = (new Line(linecounter, "", "SWR","000", "000", LOCATION_COUNTER, "000", "000000000000"));
				currentline.addError(608);
				LOCATION_COUNTER++;
    		}
    		else if (tokens[2].contains("(")) {
    			// ERROR: CANT HAVE INDEXED MEMORY REFS
				currentline = (new Line(linecounter, "", "SWR","000", "000", LOCATION_COUNTER, "000", "000000000000"));
				currentline.addError(616);
				LOCATION_COUNTER++;
    		}
    		else if ((tokens[2].contains("+") || tokens[2].contains("-") && tokens[2].charAt(0) != '=')) {
    			// ERROR: EXPRESSIONS ARE NOT ALLOWED TO BE USED AS AN OPERAND IN THIS INSTRUCTION
				currentline = (new Line(linecounter, "", "SWR","000", "000", LOCATION_COUNTER, "000", "000000000000"));
				currentline.addError(625);
				LOCATION_COUNTER++;
    		}
 

    		else if (tokens[2].contains("!") || tokens[2].contains("?") || tokens[2].contains("#") || 
    				tokens[2].contains("_") || tokens[2].contains("/") || tokens[2].contains("\"") || 
    				tokens[2].contains(".") || tokens[2].contains("@") || tokens[2].contains("$") ||
    				tokens[2].contains("%") || tokens[2].contains("^") || tokens[2].contains("&") ||
    				tokens[2].contains("[") || tokens[2].contains("]") || tokens[2].contains("{") ||
    				tokens[2].contains("}") || tokens[2].contains("<") || tokens[2].contains(">")) {
				// ERROR: ADDRESS CONSTANT CONTAINS INVALID CHARACTERS 
    			currentline = (new Line(linecounter, "", "SWR","000", "000", LOCATION_COUNTER, "000", "000000000000"));
    			currentline.addError(621); 
    			LOCATION_COUNTER++;
    		}
    		else {
    			String[] rlabels = tokens[2].split(",");
    			int rlabel_count = rlabels.length;
    			if (rlabels.length > 4) {

    				//ERROR: ONLY FOUR LABELS ARE ALLOWED TO BE DECLARED IN DIRECTIVES EXT OR ENT
    				currentline = (new Line(linecounter, "", "SWR","000", "000", LOCATION_COUNTER, "000", "000000000000"));
    				LOCATION_COUNTER++;

    				// Print error for a present label that't not allowed
    				if (invalid_label) {
    					currentline.addError(160);
    				}
    				currentline.addError(161);
    			}
    			else {
    				updateSymbolTable(tokens);
    				if (!tokens[0].equals("")) {
    					currentline.addError(160);
    				}

    				if (rlabel_count == 1) {
    					currentline = (new Line(linecounter, "", "EXT", rlabels[0], LOCATION_COUNTER, "000", "000000000000"));
    				}
    				else if (rlabel_count == 2) {
    					currentline = (new Line(linecounter, "", "EXT",rlabels[0], rlabels[1], LOCATION_COUNTER, "000", "000000000000"));
    				}
    				else if (rlabel_count == 3) {
    					currentline = (new Line(linecounter, "", "EXT", rlabels[0], rlabels[1], rlabels[2], LOCATION_COUNTER, "000", "000000000000"));
    				}
    				else if (rlabel_count == 4) {
    					currentline = (new Line(linecounter, "", "EXT", rlabels[0], rlabels[1], rlabels[2], rlabels[3], LOCATION_COUNTER, "000", "000000000000"));
    				}
    				int i = 0;
    				while (i < rlabels.length) {
    					checkForValidLabel(rlabels[i]);
    					i++;
    				}
    			}
    		}
    	}
    	else {
    		// ERROR: DIRECTIVE USED CONTAINS NO REQUIRED OPERANDS
    		currentline = (new Line(linecounter, "", "SWR","000", "000", LOCATION_COUNTER, "000", "000000000000"));
    		currentline.addError(010);
    		LOCATION_COUNTER++;
    	}
    }
    
    private static void parseAndCheckEND (String[] tokens) {
    	String prgm_name = "";
    	Boolean valid_end_label = true;

    	
    	if (tokens.length > 2) {
    	}
    	else {
    		continue_assembly = false;
    		valid_end_label = false;
    		valid_end = false;
    	}
    	
    	if (!tokens[0].equals("")) {
    		//ERROR: UNABLE TO ASSIGN LABEL TO THE END DIRECTIVE
			currentline.addError(160);
			valid_end = true;
    	}
    		
    	for (Symbol s : SYMBOL_TABLE) {
    		if (s.GetUsage().equals("PRGM")) {
    			prgm_name = s.GetLabel();
    		}
    	}
    	if (valid_start && tokens.length > 2) {
    		if (!tokens[2].equals(prgm_name)) {
    			//ERROR: LABEL THAT FOLLOWS END MUST MATCH PRGM_NAME
    			currentline.addError(162);
    			valid_end_label = false;
    			valid_end = false;
    			continue_assembly = false;
    		}
    		else {
    			currentline = (new Line(linecounter, "", "END", prgm_name, LOCATION_COUNTER, "000", "000000000000"));
    	    	valid_end = true;
    		}
    	}


    	
    }

    private static void parseAndCheckAEXS(String[] tokens) {
       	Boolean valid_AEXS = true;
        // Check if required label is present        
        if (!tokens[0].equals("")) {
            
            // Check label for correct formatting
            if (!isValidLabel(tokens[0])) {
				currentline = (new Line(linecounter, "", "SWR","000", "000", LOCATION_COUNTER, "000", "000000000000"));
				LOCATION_COUNTER++;
				
				// Print label errors
                checkForValidLabel(tokens[0]);
                valid_AEXS = false;
            }
        }
        else if (tokens.length > 2){
            //ERROR: DIRECTIVE DOES NOT HAVE REQUIRED LABEL
			currentline = (new Line(linecounter, "", "SWR","000", "000", LOCATION_COUNTER, "000", "000000000000"));
			currentline.addError(282);
			LOCATION_COUNTER++;
        	valid_AEXS = false;
        	
        }
        else {
        	//ERROR: DIRECTIVE DOES NOT HAVE REQUIRED LABEL
			currentline = (new Line(linecounter, "", "SWR","000", "000", LOCATION_COUNTER, "000", "000000000000"));
			currentline.addError(282);
			LOCATION_COUNTER++;
        	valid_AEXS = false;
        }
    
    	// Check for empty operand field
    	if (tokens.length > 2 && valid_AEXS) {
    		if (isNumber(tokens[2])) {
    			if (!(Double.valueOf(tokens[2]) >= 0) || !(Double.valueOf(tokens[2])<= 4095)) {
    				// ERROR: 115
    				currentline = (new Line(linecounter, "", "SWR","000", "000", LOCATION_COUNTER, "000", "000000000000"));
    				currentline.addError(115);
    				LOCATION_COUNTER++;
    			}
    			else {
    	    		currentline = (new Line(linecounter, tokens[0], "AEXS", tokens[2], LOCATION_COUNTER, "000", "000000000000"));
    				updateSymbolTable(tokens);
    				alternate_start = true;
    			}
    		}
    		else if (tokens[2].contains(",")) {
    			// ERROR: AEXS CANT HAVE COMMAS
				currentline = (new Line(linecounter, "", "SWR","000", "000", LOCATION_COUNTER, "000", "000000000000"));
				currentline.addError(623);
				LOCATION_COUNTER++;
    		}
    		else if (tokens[2].charAt(0) == '-') {
    			//ERROR: MEMORY REFERENCE OUT OF BOUNDS 
				currentline = (new Line(linecounter, "", "SWR","000", "000", LOCATION_COUNTER, "000", "000000000000"));
				currentline.addError(115);
				LOCATION_COUNTER++;
    		}
    		else if (tokens[2].contains("(") || tokens[2].contains(")")) {
    			// ERROR: DIRECTIVES ARE NOT ALLOWED TO USE MEMORY REFERENCES WITH AN INDEX: LINE WILL BE REPLACED BY A NOP
				currentline = (new Line(linecounter, "", "SWR","000", "000", LOCATION_COUNTER, "000", "000000000000"));
				currentline.addError(616);
				LOCATION_COUNTER++;
    		}
    		else if (isLiteral(tokens[2])) {
				currentline = (new Line(linecounter, "", "SWR","000", "000", LOCATION_COUNTER, "000", "000000000000"));
				currentline.addError(608);
				LOCATION_COUNTER++;
    		}
    		else if (tokens[2].charAt(0) == '*') {
    			if (tokens[2].length() > 1) {
    				// ERROR: ALTERNATE EXECUTION STARTS CANNOT USE EXPRESSIONS WITH STAR NOTATION
    				currentline = (new Line(linecounter, "", "SWR","000", "000", LOCATION_COUNTER, "000", "000000000000"));
    				currentline.addError(313);
    				LOCATION_COUNTER++;
    			}
    			else {
		    		currentline = (new Line(linecounter, tokens[0], "AEXS",tokens[2], LOCATION_COUNTER, "000", "000000000000"));
		    		alternate_start = true;
		    		alternate_start_location = Integer.toString(LOCATION_COUNTER);
		    		updateSymbolTable(tokens);
    			}
    			
    			
    		}
    		else if (SYMBOL_TABLE.symbolIsDefined(tokens[2])) {
    			if (!(Double.valueOf(SYMBOL_TABLE.getString(tokens[2])) >= 0) || 
    					!(Double.valueOf(SYMBOL_TABLE.getString(tokens[2])) <= 4095)) {
    				// ERROR: Symbol represents value out of range
    				currentline = (new Line(linecounter, "", "SWR","000", "000", LOCATION_COUNTER, "000", "000000000000"));
    				currentline.addError(232);
    				LOCATION_COUNTER++;
    			}
    			else {
		    		currentline = (new Line(linecounter, "", "AEXS",tokens[2], LOCATION_COUNTER, "000", "000000000000"));
					alternate_start = true;
					alternate_start_location = SYMBOL_TABLE.getString(tokens[2]);
		    		updateSymbolTable(tokens);
    			}
    		}
    		else {
    			// WARNING: SYMBOL HAS NOT BEEN DEFINED WILL BE CHECKED IN 
	    		currentline = (new Line(linecounter, "", "AEXS",tokens[2], LOCATION_COUNTER, "000", "000000000000"));
	    		currentline.addError(310);
				alternate_start = true;
				alternate_start_location = tokens[2];
	    		updateSymbolTable(tokens);
    		}
    	}
    	else if (valid_AEXS) {
			currentline = (new Line(linecounter, "", "SWR","000", "000", LOCATION_COUNTER, "000", "000000000000"));
			currentline.addError(010);
			LOCATION_COUNTER++;
    	}
    }
    
    private static void parseAndCheckSKS (String[] tokens) {
         if (tokens.length > 2) {
         	if (isNumber(tokens[2])) {
         		// NEED TO CHANGE TO CHECK IF AMOUNT ADDED SURPASSES PROGRAM LENGTH!!!!
         		if (!(Double.valueOf(tokens[2]) >= 0) || !(Double.valueOf(tokens[2]) <= 4095)) {
         			
         			//ERROR: SKIP STORAGE AMOUNT EXCEEDS MEMORY LIMIT
        			currentline = (new Line(linecounter, "", "SWR","000", "000", LOCATION_COUNTER, "000", "000000000000"));
        			currentline.addError(170);
        			LOCATION_COUNTER++;
         		}
         		else {
        			if (!tokens[0].equals("") && isValidLabel(tokens[0])) {
        				currentline = (new Line(linecounter, tokens[0], "SKS",tokens[2], LOCATION_COUNTER, "000", "000000000000"));
        				updateSymbolTable(tokens);
        			}
        			else if (!tokens[0].equals("")){
        				currentline = (new Line(linecounter, "", "SKS",tokens[2], LOCATION_COUNTER, "000", "000000000000"));
        				checkForValidLabel(tokens[0]);
        			}
        			else {
        				currentline = (new Line(linecounter, "", "SKS",tokens[2], LOCATION_COUNTER, "000", "000000000000"));
        			}
        			LOCATION_COUNTER += Integer.valueOf(tokens[2]);
         		}
         	}
         	else if (tokens[2].charAt(0) == '-') {
         		// ERROR: SKIP STORAGE
    			currentline = (new Line(linecounter, "", "SWR","000", "000", LOCATION_COUNTER, "000", "000000000000"));
    			currentline.addError(629);
    			LOCATION_COUNTER++;
         	}
         	else {
         		//ERROR: INCORRECT OPERAND FORMAT
    			currentline = (new Line(linecounter, "", "SWR","000", "000", LOCATION_COUNTER, "000", "000000000000"));
    			currentline.addError(141);
    			LOCATION_COUNTER++;
         	}
         }
         else {
         	// ERROR: DIRECTIVE USED CONTAINS NO REQUIRED OPERANDS
 			currentline = (new Line(linecounter, "", "SWR","000", "000", LOCATION_COUNTER, "000", "000000000000"));
 			currentline.addError(010);
 			LOCATION_COUNTER++;
         }
    }
    
    private static void parseAndCheckCHC(String[] tokens) {
    	boolean valid_chc = true, valid_memory_ref = true;
    	int i = 0;

    	if (tokens.length > 2) {
    		if (tokens[2].contains(",")) {
				// ERROR: COMMAS ARE NOT VALID SEPARATORS
				currentline = (new Line(linecounter, "", "SWR","000", "000", LOCATION_COUNTER, "000", "000000000000"));
				currentline.addError(116);
				LOCATION_COUNTER++;
			}
			else if (tokens[2].contains("!") || tokens[2].contains("?") || tokens[2].contains("#") || 
    				tokens[2].contains("_") || tokens[2].contains("/") || tokens[2].contains("\"") || 
    				tokens[2].contains(".") || tokens[2].contains("@") || tokens[2].contains("$") ||
    				tokens[2].contains("%") || tokens[2].contains("^") || tokens[2].contains("&") ||
    				tokens[2].contains("[") || tokens[2].contains("]") || tokens[2].contains("{") ||
    				tokens[2].contains("}") || tokens[2].contains("<") || tokens[2].contains(">")) {
				// ERROR: ADDRESS CONSTANT CONTAINS INVALID CHARACTERS 
    			currentline = (new Line(linecounter, "", "SWR","000", "000", LOCATION_COUNTER, "000", "000000000000"));
    			currentline.addError(101); 
    			LOCATION_COUNTER++;
			}
			else if (tokens[2].contains("+") || tokens[2].contains("-")) {
				// ERROR: CANT USE EXPRESSION
				currentline = (new Line(linecounter, "", "SWR","000", "000", LOCATION_COUNTER, "000", "000000000000"));
				currentline.addError(670);
				LOCATION_COUNTER++;
				valid_memory_ref = false;
			}
			else if (tokens[2].contains("=")) {
				// ERROR: CANT USE EXPRESSION
				currentline = (new Line(linecounter, "", "SWR","000", "000", LOCATION_COUNTER, "000", "000000000000"));
				currentline.addError(671);
				LOCATION_COUNTER++;
				valid_memory_ref = false;
			}
			else if (tokens[2].charAt(0) == '*') {
				// ERROR: STAR CANT BE FIRST CHARACTER IN CHC OPERAND FIELD
				currentline = (new Line(linecounter, "", "SWR","000", "000", LOCATION_COUNTER, "000", "000000000000"));
				currentline.addError(672);
				LOCATION_COUNTER++;
				valid_memory_ref = false;
				
			}
    		// Check CHC with reserved memory
			else if (tokens[2].contains("*")) {
    			while (tokens[2].charAt(i) != '*') {
    				i++;
    			}
    			String operand1 = tokens[2].substring(0, i);
    			String operand2 = tokens[2].substring(i+1);

    			// Check reserve memory operand
    			if (isNumber(operand1)) {
    				if (!(Double.valueOf(operand1) >= 0) || !(Double.valueOf(operand1) <= 4095)) {
    					//ERROR: DIRECTIVE NUMERIC OUT OF RANGE
    					currentline = (new Line(linecounter, "", "SWR","000", "000", LOCATION_COUNTER, "000", "000000000000"));
    					currentline.addError(115);
    					LOCATION_COUNTER++;
    					valid_memory_ref = false;
    				}
    			}
				
    			else if (operand1.charAt(0) == '-') {
					currentline = (new Line(linecounter, "", "SWR","000", "000", LOCATION_COUNTER, "000", "000000000000"));
					currentline.addError(115);
					LOCATION_COUNTER++;
					valid_memory_ref = false;
    			}
    			else if (operand1.contains("(") || operand1.contains(")")) {
    				// ERROR: MEMORY REFERENCES IN CHC CANNOT USE INDEX
					currentline = (new Line(linecounter, "", "SWR","000", "000", LOCATION_COUNTER, "000", "000000000000"));
					currentline.addError(669);
					LOCATION_COUNTER++;
					valid_memory_ref = false;
    			}
    			else if (operand1.contains("*")) {
    				// ERROR: CAN't USE STAR
					currentline = (new Line(linecounter, "", "SWR","000", "000", LOCATION_COUNTER, "000", "000000000000"));
					currentline.addError(668);
					LOCATION_COUNTER++;
					valid_memory_ref = false;
    			}
    			
    			else {
    				//ERROR: INCORRECT OPERAND FORMAT
    				currentline = (new Line(linecounter, "", "SWR","000", "000", LOCATION_COUNTER, "000", "000000000000"));
    				currentline.addError(141);
    				LOCATION_COUNTER++;
    				valid_memory_ref = false;
    			}
    			if (valid_memory_ref) {
    				// Check character constant
    				if ((operand2.length() < 2) || (operand2.charAt(0) != '\'' && operand2.charAt(operand2.length()-1) != '\'') || operand2.contains("\"")) {

    					//ERROR: CHARACTER CONSTANTS MUST BE SURROUNDED BY SINGLE QUOTES
    					currentline = (new Line(linecounter, "", "SWR","000", "000", LOCATION_COUNTER, "000", "000000000000"));
    					currentline.addError(117);
    					LOCATION_COUNTER++;
    				}
    				else {
    					if (operand2.length() > 2) {
    	    				String characters = operand2.substring(1, operand2.length()-1);
    	    				if ((operand2.length() < 2) || (operand2.charAt(0) != '\'' && operand2.charAt(tokens[2].length()-1) != '\'') || operand2.contains("\"")) {
    	        				//ERROR: CHARACTER CONSTANTS MUST BE SURROUNDED BY SINGLE QUOTES
    	        	 			currentline = (new Line(linecounter, "", "SWR","000", "000", LOCATION_COUNTER, "000", "000000000000"));
    	        	 			currentline.addError(117);
    	        	 			LOCATION_COUNTER++;
    	        			}
    						// Check character length
    	    				else if (characters.length() > 4) {

    							//ERROR: CHARACTER CONSTANTS CAN ONLY BE 1-4 CHARACTERS LONG
    							currentline = (new Line(linecounter, "", "SWR","000", "000", LOCATION_COUNTER, "000", "000000000000"));
    							currentline.addError(118);
    							LOCATION_COUNTER++;
    						}

    						// Check for valid character values
    						else {
    							i = 0;
    							while (i < characters.length()) {
    								if (Character.isLowerCase(characters.charAt(i)) || Character.isUpperCase(characters.charAt(i))) {
    									valid_chc = true;
    								}
    								i++;
    							}
    							// VALID CHARACTER WITH RESERVE MEMORY
    							if (valid_chc) {
    		        				// Add valid line to intermediate
    								if (!tokens[0].equals("") && isValidLabel(tokens[0])) {
        								currentline = (new Line(linecounter, tokens[0], "CHC",characters, LOCATION_COUNTER, "000", "000000000000"));
        								updateSymbolTable(tokens);
    								}
    								else if (!tokens[0].equals("")) {
        								currentline = (new Line(linecounter, "", "CHC",characters, LOCATION_COUNTER, "000", "000000000000"));
        								checkForValidLabel(tokens[0]);
    								}
    								else {
        								currentline = (new Line(linecounter, "", "CHC",characters, LOCATION_COUNTER, "000", "000000000000"));
    								}
  								
    								LOCATION_COUNTER += Integer.valueOf(operand1);

    							}
    							else {
    								// ERROR: INVALID CHARACTER VALUE, CHC CHARACTERS MUST BE ALL UPPER AND LOWER CASE LETTERS
    								currentline = (new Line(linecounter, "", "SWR","000", "000", LOCATION_COUNTER, "000", "000000000000"));
    								currentline.addError(286);
    								LOCATION_COUNTER++;
    							}
    						}
    					}
    					else {
    						// ERROR: CHARACTER CONSTANT IS EMPTY!
    						currentline = (new Line(linecounter, "", "SWR","000", "000", LOCATION_COUNTER, "000", "000000000000"));
    						currentline.addError(113);
    						LOCATION_COUNTER++;
    					}
    				}
    			}
    		}
    		else {
    			
    			// Check for character quotes surrounding character
    			if ((tokens[2].length() < 2) || (tokens[2].charAt(0) != '\'' && tokens[2].charAt(tokens[2].length()-1) != '\'') || tokens[2].contains("\"")) {
    				//ERROR: CHARACTER CONSTANTS MUST BE SURROUNDED BY SINGLE QUOTES
    	 			currentline = (new Line(linecounter, "", "SWR","000", "000", LOCATION_COUNTER, "000", "000000000000"));
    	 			currentline.addError(117);
    	 			LOCATION_COUNTER++;
    			}
    			else {
    				String characters = tokens[2].substring(1, tokens[2].length()-1);

    				// Check for valid length
    				if (characters.length() > 4) {
    					//ERROR: CHARACTER CONSTANTS CAN ONLY BE 1-4 CHARACTERS LONG
        	 			currentline = (new Line(linecounter, "", "SWR","000", "000", LOCATION_COUNTER, "000", "000000000000"));
        	 			currentline.addError(118);
        	 			LOCATION_COUNTER++;
    				}

    				// Check for valid characters
    				else {
    					i = 0;
    					while (i < characters.length()) {
    						if (Character.isLowerCase(characters.charAt(i)) || Character.isUpperCase(characters.charAt(i))) {
    							valid_chc = true;
    						}
    						i++;
    					}
    					if (!valid_chc) {
    						// ERROR: INVALID CHARACTER VALUE, CHC CHARACTERS MUST BE ALL UPPER AND LOWER CASE LETTERS
    	    	 			currentline = (new Line(linecounter, "", "SWR","000", "000", LOCATION_COUNTER, "000", "000000000000"));
    	    	 			currentline.addError(286);
    	    	 			LOCATION_COUNTER++;
    					}

    					// VALID CHARACTER WITH NO RESERVE SPACE
    					else {
    	    	 			
    	    	 			if (!tokens[0].equals("") && isValidLabel(tokens[0])) {
								currentline = (new Line(linecounter, tokens[0], "CHC",asciiToHex(characters).toUpperCase(), LOCATION_COUNTER, "000", "000000000000"));
								updateSymbolTable(tokens);
							}
							else if (!tokens[0].equals("")) {
								currentline = (new Line(linecounter, "", "CHC",asciiToHex(characters).toUpperCase(), LOCATION_COUNTER, "000", "000000000000"));
								checkForValidLabel(tokens[0]);
							}
							else {
								currentline = (new Line(linecounter, "", "CHC",asciiToHex(characters).toUpperCase(), LOCATION_COUNTER, "000", "000000000000"));
							}
    	    	 			LOCATION_COUNTER++;
    					}
    				}
    			}
    		}
    	}
    	else {
    		// ERROR: DIRECTIVE USED CONTAINS NO REQUIRED OPERANDS
    		currentline = (new Line(linecounter, "", "SWR","000", "000", LOCATION_COUNTER, "000", "000000000000"));
    		currentline.addError(010);  
    		LOCATION_COUNTER++;
    		}
    }
    
    private static void parseAndCheckNum (String[] tokens) {
    	Boolean valid_num = true;

        if (tokens.length > 2) {
        	if (hasMultipleOperands(tokens[2])) {
        		int i = 0;
        		if (tokens[2].contains(",")) {
        			//ERROR: COMMAS ARE INVALID SEPARATORS FOR THIS DIRECTIVE
    	 			currentline = (new Line(linecounter, "", "SWR","000", "000", LOCATION_COUNTER, "000", "000000000000"));
    	 			currentline.addError(116);
    	 			LOCATION_COUNTER++;
        		}
        		else if (tokens[2].contains("*") && tokens[2].charAt(0) == '*') {
        			//ERROR: STAR NOTATION CANT BE USED
    	 			currentline = (new Line(linecounter, "", "SWR","000", "000", LOCATION_COUNTER, "000", "000000000000"));
    	 			currentline.addError(622);
    	 			LOCATION_COUNTER++;
        		}
        		
        		else if (tokens[2].contains("*")){
        			while (tokens[2].charAt(i) != '*') {
        				i++;
        			}
        			String operand1 = tokens[2].substring(0, i);
        			String operand2 = tokens[2].substring(i+1);

        			// Check memory reserve space
        			if (isNumber(operand1)) {
        				if (!(Double.valueOf(operand1) >= 0) || !(Double.valueOf(operand1) <= 4095)) {
        					//ERROR: DIRECTIVE NUMERIC OUT OF RANGE
        					currentline = (new Line(linecounter, "", "SWR","000", "000", LOCATION_COUNTER, "000", "000000000000"));
        					currentline.addError(115);
        					LOCATION_COUNTER++;
        					valid_num = false;
        				}
        			}
        			else if (operand1.charAt(0) == '-') {
        				// ERROR: MEMORY REFERENCE OUT OF RANGE
    					currentline = (new Line(linecounter, "", "SWR","000", "000", LOCATION_COUNTER, "000", "000000000000"));
    					currentline.addError(115);
    					LOCATION_COUNTER++;
    					valid_num = false;
        			}
        			else if (isLiteral(operand1)) {
        				// ERROR: USE OF LITERALS IS NOT ALLOWED
    					currentline = (new Line(linecounter, "", "SWR","000", "000", LOCATION_COUNTER, "000", "000000000000"));
    					currentline.addError(608);
    					LOCATION_COUNTER++;
    					valid_num = false;
        			}
        			else {
        				//ERROR: INCORRECT OPERAND FORMAT
        				currentline = (new Line(linecounter, "", "SWR","000", "000", LOCATION_COUNTER, "000", "000000000000"));
        				currentline.addError(141);
        				LOCATION_COUNTER++;
        				valid_num = false;
        			}

        			// Check numerical constant if memory reference is valid
        			if (valid_num) {
        				if (isNumber(operand2)) {
        					if (!(Long.valueOf(operand2) >= (-Math.pow(2,31))) || !(Long.valueOf(operand2) <= (Math.pow(2,31)-1))) {
        						//ERROR: NUMBER CONSTANT MUST BE A BASE 10 NUMBER IN RANGE -2*10^31 to 2*10^31-1
        						currentline = (new Line(linecounter, "", "SWR","000", "000", LOCATION_COUNTER, "000", "000000000000"));
        						currentline.addError(180);
        						LOCATION_COUNTER++;
        					}
        					else {
        						// VALID NUMERIC CONSTANT WITH MEMORY REF

        						
        						if (!tokens[0].equals("") && isValidLabel(tokens[0])) {
    								currentline = (new Line(linecounter, tokens[0], "NUM", operand1, operand2, LOCATION_COUNTER, "000", "000000000000"));
    								updateSymbolTable(tokens);
								}
								else if (!tokens[0].equals("")) {
    								currentline = (new Line(linecounter, "", "NUM",operand1, operand2, LOCATION_COUNTER, "000", "000000000000"));
    								checkForValidLabel(tokens[0]);
								}
								else {
    								currentline = (new Line(linecounter, "", "NUM",operand1, operand2, LOCATION_COUNTER, "000", "000000000000"));
								}

        						LOCATION_COUNTER += Integer.valueOf(operand1);

        					}
        				}
        				else if (operand2.contains("+")) {
        					// ERROR: EXPRESSIONS NOT ALLOWED IN THIS DIRECTIVE
        					currentline = (new Line(linecounter, "", "SWR","000", "000", LOCATION_COUNTER, "000", "000000000000"));
        					currentline.addError(625);
        					LOCATION_COUNTER++;
        				}
        				else if (operand2.charAt(0) == '-') {
        					String operand2_temp = operand2.substring(1);
        					int number = 0-Integer.valueOf(operand2_temp);
        					if (number >= -Math.pow(2,31) && number <= Math.pow(2,31)) {
        						// VALID NEGATIVE NUMERIC CONSTANT
        						
        						if (!tokens[0].equals("") && isValidLabel(tokens[0])) {
    								currentline = (new Line(linecounter, tokens[0], "NUM", operand1, operand2, LOCATION_COUNTER, "000", "000000000000"));
    								updateSymbolTable(tokens);
								}
								else if (!tokens[0].equals("")) {
    								currentline = (new Line(linecounter, "", "NUM",operand1, operand2, LOCATION_COUNTER, "000", "000000000000"));
    								checkForValidLabel(tokens[0]);
								}
								else {
    								currentline = (new Line(linecounter, "", "NUM",operand1, operand2, LOCATION_COUNTER, "000", "000000000000"));
								}

        						LOCATION_COUNTER += Integer.valueOf(operand1);
        					}
        				}
        				else if (operand2.contains("-") && operand2.charAt(0) != '-') {
        					// ERROR: EXPRESSIONS NOT ALLOWED
        					currentline = (new Line(linecounter, "", "SWR","000", "000", LOCATION_COUNTER, "000", "000000000000"));
        					currentline.addError(625);
        					LOCATION_COUNTER++;
        				}
        				else {
        					//ERROR: NUMBER CONSTANT MUST BE A NUMERICAL VALUE
        					currentline = (new Line(linecounter, "", "SWR","000", "000", LOCATION_COUNTER, "000", "000000000000"));
        					currentline.addError(181);
        					LOCATION_COUNTER++;
        				}
        			}
        		}
        	}
        	// Check syntax for NUM with one operand
        	else {
        		if (isNumber(tokens[2])) {
        			if (!(Long.valueOf(tokens[2]) >= (-Math.pow(2,31))) || !(Long.valueOf(tokens[2]) <= (Math.pow(2,31)-1))) {
        				//ERROR: NUMBER CONSTANT MUST BE A BASE 10 NUMBER IN RANGE -2*10^31 to 2*10^31-1
        	 			currentline = (new Line(linecounter, "", "SWR","000", "000", LOCATION_COUNTER, "000", "000000000000"));
        	 			currentline.addError(180);
        	 			LOCATION_COUNTER++;
        			}
        			// VALID NUMERICAL CONSTANT
        			else {
						if (!tokens[0].equals("") && isValidLabel(tokens[0])) {
							currentline = (new Line(linecounter, tokens[0], "NUM",tokens[2], LOCATION_COUNTER, "000", "000000000000"));
							updateSymbolTable(tokens);
						}
						else if (!tokens[0].equals("")) {
							currentline = (new Line(linecounter, "", "NUM",tokens[2], LOCATION_COUNTER, "000", "000000000000"));
							checkForValidLabel(tokens[0]);
						}
						else {
							currentline = (new Line(linecounter, "", "NUM",tokens[2], LOCATION_COUNTER, "000", "000000000000"));
						}
						LOCATION_COUNTER++;
        			}
        		}
        		else if (isLiteral(tokens[2])) {
        			// ERROR: USE OF LITERALS IS NOT ALLOWED
					currentline = (new Line(linecounter, "", "SWR","000", "000", LOCATION_COUNTER, "000", "000000000000"));
					currentline.addError(608);
					LOCATION_COUNTER++;
        		}
        		else if (tokens[2].contains("+")) {
					// ERROR: EXPRESSIONS NOT ALLOWED IN THIS DIRECTIVE
					currentline = (new Line(linecounter, "", "SWR","000", "000", LOCATION_COUNTER, "000", "000000000000"));
					currentline.addError(625);
					LOCATION_COUNTER++;
				}
        		else if (tokens[2].contains("-") && tokens[2].charAt(0) != '-') {
					// ERROR: EXPRESSIONS NOT ALLOWED
					currentline = (new Line(linecounter, "", "SWR","000", "000", LOCATION_COUNTER, "000", "000000000000"));
					currentline.addError(625);
					LOCATION_COUNTER++;
				}
        		else if (tokens[2].contains("++") || tokens[2].contains("--") || tokens[2].contains("+-") || tokens[2].contains("-+")) {
        			// ERROR: BACK TO BACK OPERATORS
					currentline = (new Line(linecounter, "", "SWR","000", "000", LOCATION_COUNTER, "000", "000000000000"));
					currentline.addError(311);
					LOCATION_COUNTER++;
        		}
        		else if (tokens[2].charAt(0) == '-' && tokens[2].length() > 1) {
        			String operand_temp = tokens[2].substring(1);
					long number = 0-Long.valueOf(operand_temp);
					if (number >= -Math.pow(2,31) && number <= Math.pow(2,31)) {
						// VALID NEGATIVE NUMERIC CONSTANT
						int number_int = (int)number;
						
						if (!tokens[0].equals("") && isValidLabel(tokens[0])) {
							currentline = (new Line(linecounter, tokens[0], "NUM", tokens[2], LOCATION_COUNTER, "000", "000000000000"));
							updateSymbolTable(tokens);
						}
						else if (!tokens[0].equals("")) {
							currentline = (new Line(linecounter, "", "NUM",tokens[2], LOCATION_COUNTER, "000", "000000000000"));
							checkForValidLabel(tokens[0]);
						}
						else {
							currentline = (new Line(linecounter, "", "NUM",tokens[2], LOCATION_COUNTER, "000", "000000000000"));
						}

						LOCATION_COUNTER++;
					}
					else {
						//ERROR: NUMBER CONSTANT MUST BE A BASE 10 NUMBER IN RANGE -2*10^31 to 2*10^31-1
        	 			currentline = (new Line(linecounter, "", "SWR","000", "000", LOCATION_COUNTER, "000", "000000000000"));
        	 			currentline.addError(180);
        	 			LOCATION_COUNTER++;
					}
        		}
        		else {
        			//ERROR: NUMBER CONSTANT MUST BE A NUMERICAL VALUE
    	 			currentline = (new Line(linecounter, "", "SWR","000", "000", LOCATION_COUNTER, "000", "000000000000"));
    	 			currentline.addError(181);
    	 			LOCATION_COUNTER++;
        		}
        	}
        }      
        else {
        	// ERROR: DIRECTIVE USED CONTAINS NO REQUIRED OPERANDS
    		currentline = (new Line(linecounter, "", "SWR","000", "000", LOCATION_COUNTER, "000", "000000000000"));
    		currentline.addError(010);        	
    		LOCATION_COUNTER++;
        }
    }


    private static void parseAndCheckADC (String[] tokens) {
    	if (tokens.length > 2){
    		if (tokens[2].contains(",")) {
    			// ERROR: ADC CANNOT CONTAIN COMMAS
    			currentline = (new Line(linecounter, "", "SWR","000", "000", LOCATION_COUNTER, "000", "000000000000"));
    			currentline.addError(613);
    			LOCATION_COUNTER++;
    		}
    		else if (tokens[2].contains("(") || tokens[2].contains(")")) {
    			// ERROR: DIRECTIVES ARE NOT ALLOWED TO USE MEMORY REFERENCES WITH AN INDEX: LINE WILL BE REPLACED BY A NOP
    			currentline = (new Line(linecounter, "", "SWR","000", "000", LOCATION_COUNTER, "000", "000000000000"));
    			currentline.addError(616);
    			LOCATION_COUNTER++;
    		}
    		else if (isLiteral(tokens[2])) {
    			// ERROR: ADC CANNOT USE LITERALS
    			currentline = (new Line(linecounter, "", "SWR","000", "000", LOCATION_COUNTER, "000", "000000000000"));
    			currentline.addError(614);    		
    			LOCATION_COUNTER++;
    		}
    		else if (tokens[2].length() == 1 && (tokens[2].charAt(0) == '+' || tokens[2].charAt(0) == '-')) {
    			// AN ADDRESS CONSTANT STARTING WITH + OR - MUST BE FOLLOWED BY A NUMERICAL VALUE
    			currentline = (new Line(linecounter, "", "SWR","000", "000", LOCATION_COUNTER, "000", "000000000000"));
    			currentline.addError(620); 
    			LOCATION_COUNTER++;
    		}
    		else if (tokens[2].contains("!") || tokens[2].contains("?") || tokens[2].contains("#") || 
    				tokens[2].contains("_") || tokens[2].contains("/") || tokens[2].contains("\"") || 
    				tokens[2].contains(".") || tokens[2].contains("@") || tokens[2].contains("$") ||
    				tokens[2].contains("%") || tokens[2].contains("^") || tokens[2].contains("&") ||
    				tokens[2].contains("[") || tokens[2].contains("]") || tokens[2].contains("{") ||
    				tokens[2].contains("}") || tokens[2].contains("<") || tokens[2].contains(">")) {
				// ERROR: ADDRESS CONSTANT CONTAINS INVALID CHARACTERS 
    			currentline = (new Line(linecounter, "", "SWR","000", "000", LOCATION_COUNTER, "000", "000000000000"));
    			currentline.addError(621); 
    			LOCATION_COUNTER++;

			}
    		else {
    			// Check if * notation
    			if (tokens[2].charAt(0) == '*') {

    				if (tokens[2].length() > 1) {
    					String after_star = tokens[2].substring(1);

    					// Check for multiple stars
    					if (after_star.contains("*")) {

    						//ERROR: ONLY ONE STAR ALLOWED
    						currentline = (new Line(linecounter, "", "SWR","000", "000", LOCATION_COUNTER, "000", "000000000000"));
    						currentline.addError(307);
    						LOCATION_COUNTER++;
    					}
    					else {
    						if (tokens[2].charAt(1) == '+' || tokens[2].charAt(1) == '-') {
    							boolean negative = false;
    							if (tokens[2].charAt(1) == '-') {
    								negative = true;
    							}
    							if (tokens[2].length() > 2) {
    								String after_operator = tokens[2].substring(2);
    								if (after_operator.contains("+") || after_operator.contains("-")) {
    									// ERROR: ADC CANNOT CONTAIN EXPRESSIONS
    									currentline = (new Line(linecounter, "", "SWR","000", "000", LOCATION_COUNTER, "000", "000000000000"));
    									currentline.addError(615);  
    									LOCATION_COUNTER++;
    								}
    								else {
    									if (isNumber(tokens[2].substring(2))) {		
    										int result = 0;
    										String number = tokens[2].substring(2);
    										if (negative) {
        										result = LOCATION_COUNTER - Integer.valueOf(number);

    										}
    										else {
        										result = LOCATION_COUNTER + Integer.valueOf(number);
    										}
    										if (result >= 0 && result <= 4095) {
    											// Add valid line to intermediate
    											if (!tokens[0].equals("") && isValidLabel(tokens[0])) {
    												currentline = (new Line(linecounter, tokens[0], "ADC",Integer.toHexString(result), LOCATION_COUNTER, "000", "000000000000"));
    												updateSymbolTable(tokens);
    											}
    											else if (!tokens[0].equals("")) {
    												currentline = (new Line(linecounter, "", "ADC",Integer.toHexString(result), LOCATION_COUNTER, "000", "000000000000"));
    												checkForValidLabel(tokens[0]);
    											}
    											else {
    												currentline = (new Line(linecounter, "", "ADC",Integer.toHexString(result), LOCATION_COUNTER, "000", "000000000000"));
    											}
    											LOCATION_COUNTER++;
    										}
    										else {
    	    									currentline = (new Line(linecounter, "", "SWR","000", "000", LOCATION_COUNTER, "000", "000000000000"));
    											currentline.addError(115);
    											LOCATION_COUNTER++;
    										}

    									}
    									else if (SYMBOL_TABLE.symbolIsDefined(tokens[2].substring((2))) && !SYMBOL_TABLE.getUsage(tokens[2].substring(2)).equals("EXT")) {
    										int result = 0;
    										String symbol = tokens[2].substring(2);
    										if (negative) {
        										result = LOCATION_COUNTER - Integer.valueOf(SYMBOL_TABLE.getString(symbol));

    										}
    										else {
        										result = LOCATION_COUNTER + Integer.valueOf(SYMBOL_TABLE.getString(symbol));
    										}
    										if (result >= 0 && result <= 4095) {
    											// Add valid line to intermediate
    											if (!tokens[0].equals("") && isValidLabel(tokens[0])) {
    												currentline = (new Line(linecounter, tokens[0], "ADC",Integer.toHexString(result), LOCATION_COUNTER, "000", "000000000000"));
    												updateSymbolTable(tokens);
    											}
    											else if (!tokens[0].equals("")) {
    												currentline = (new Line(linecounter, "", "ADC",Integer.toHexString(result), LOCATION_COUNTER, "000", "000000000000"));
    												checkForValidLabel(tokens[0]);
    											}
    											else {
    												currentline = (new Line(linecounter, "", "ADC",Integer.toHexString(result), LOCATION_COUNTER, "000", "000000000000"));
    											}
    											LOCATION_COUNTER++;
    										}
    										else {
    	    									currentline = (new Line(linecounter, "", "SWR","000", "000", LOCATION_COUNTER, "000", "000000000000"));
    											currentline.addError(115);
    											LOCATION_COUNTER++;
    										}
    									}
    									else if (SYMBOL_TABLE.symbolIsDefined(tokens[2].substring((2))) && SYMBOL_TABLE.getUsage(tokens[2].substring(2)).equals("EXT")) {
    										// ERROR: ADC CANNOT USE EXTERNAL LABELS
    										currentline = (new Line(linecounter, "", "SWR","000", "000", LOCATION_COUNTER, "000", "000000000000"));
    										currentline.addError(681);
    										LOCATION_COUNTER++;
    									}
    									else {
    										// ERROR: SYMBOL FOLLOWING AN OPERATOR IN ADC MUST BE PREVIOUSLY DEFINED
    										currentline = (new Line(linecounter, "", "SWR","000", "000", LOCATION_COUNTER, "000", "000000000000"));
											currentline.addError(151);
											LOCATION_COUNTER++;
    										
    									}
    								}
    							}
    							else {
    								// ERROR: AN OPERATOR AND NUMBER MUST FOLLOW *
    								currentline = (new Line(linecounter, "", "SWR","000", "000", LOCATION_COUNTER, "000", "000000000000"));
    								currentline.addError(308);
    								LOCATION_COUNTER++;
    							}
    						}
    						else if (tokens[2].length() > 2) {
    							// ERROR: AN OPERATOR AND NUMBER MUST FOLLOW *
    							currentline = (new Line(linecounter, "", "SWR","000", "000", LOCATION_COUNTER, "000", "000000000000"));
    							currentline.addError(308);
    							LOCATION_COUNTER++;
    						}
    					}
    				}
    				else {
    					// Add valid line to intermediate
    					if (!tokens[0].equals("") && isValidLabel(tokens[0])) {
    						currentline = (new Line(linecounter, tokens[0], "ADC",Integer.toHexString(LOCATION_COUNTER), LOCATION_COUNTER, "000", "000000000000"));
    						updateSymbolTable(tokens);
    					}
    					else if (!tokens[0].equals("")) {
    						currentline = (new Line(linecounter, "", "ADC",Integer.toHexString(LOCATION_COUNTER), LOCATION_COUNTER, "000", "000000000000"));
    						checkForValidLabel(tokens[0]);
    					}
    					else {
    						currentline = (new Line(linecounter, "", "ADC",Integer.toHexString(LOCATION_COUNTER), LOCATION_COUNTER, "000", "000000000000"));
    					}
    					LOCATION_COUNTER++;
    				}
    			}
    			// Check if not star notation and + or - is used
    			else if (tokens[2].contains("+") || tokens[2].contains("-")) {
    				// ERROR EXPRESSIONS ARE NOT ALLOWED IN ADDRESS CONSTANTS
    				currentline = (new Line(linecounter, "", "SWR","000", "000", LOCATION_COUNTER, "000", "000000000000"));
    				currentline.addError(615);
    				LOCATION_COUNTER++;
    			}
    			// Check if number
    			else if (isNumber(tokens[2])) {
    				// ERROR: ADDRESS CONSTANT MUST BE EITHER A PREVIOUSLY EQUATED LABEL OR * NOTATION
    				currentline = (new Line(linecounter, "", "SWR","000", "000", LOCATION_COUNTER, "000", "000000000000"));
    				currentline.addError(401);
    				LOCATION_COUNTER++;
    			}

    			// Check if equated label
    			else if (SYMBOL_TABLE.symbolIsDefined(tokens[2])) {
    				if (!SYMBOL_TABLE.getUsage(tokens[2]).equals("EXT")) {
    					if (!(Double.valueOf(SYMBOL_TABLE.getLocation(tokens[2])) >= 0) || 
    							!(Double.valueOf(SYMBOL_TABLE.getLocation(tokens[2])) <= 4095)) {
    						// ERROR: Symbol represents value out of range
    						currentline = (new Line(linecounter, "", "SWR","000", "000", LOCATION_COUNTER, "000", "000000000000"));
    						currentline.addError(232);
    						LOCATION_COUNTER++;
    					}
    					else {
    						int result = Integer.valueOf(SYMBOL_TABLE.getLocation(tokens[2]));
    						// Add valid line to intermediate
    						if (!tokens[0].equals("") && isValidLabel(tokens[0])) {
    							currentline = (new Line(linecounter, tokens[0], "ADC",Integer.toHexString(result), LOCATION_COUNTER, "000", "000000000000"));
    							updateSymbolTable(tokens);
    						}
    						else if (!tokens[0].equals("")) {
    							currentline = (new Line(linecounter, "", "ADC",Integer.toHexString(result), LOCATION_COUNTER, "000", "000000000000"));
    							checkForValidLabel(tokens[0]);
    						}
    						else {
    							currentline = (new Line(linecounter, "", "ADC",Integer.toHexString(result), LOCATION_COUNTER, "000", "000000000000"));
    						}
    						LOCATION_COUNTER++;
    					}
    				}
    				else {
    					// ERROR: ADC CANNOT USE EXTERNAL LABELS
						currentline = (new Line(linecounter, "", "SWR","000", "000", LOCATION_COUNTER, "000", "000000000000"));
						currentline.addError(681);
						LOCATION_COUNTER++;
    				}
    			}
    			else {
    				// CHECK IN PASS 2 UNDEFINED RLABEL
					// Add valid line to intermediate
					if (!tokens[0].equals("") && isValidLabel(tokens[0])) {
						currentline = (new Line(linecounter, tokens[0], "ADC",tokens[2], LOCATION_COUNTER, "000", "000000000000"));
						updateSymbolTable(tokens);
					}
					else if (!tokens[0].equals("")) {
						currentline = (new Line(linecounter, "", "ADC",tokens[2], LOCATION_COUNTER, "000", "000000000000"));
						checkForValidLabel(tokens[0]);
					}
					else {
						currentline = (new Line(linecounter, "", "ADC",tokens[2], LOCATION_COUNTER, "000", "000000000000"));
					}
					LOCATION_COUNTER++;
    			}
    		}
    	}
    	else {
    		// ERROR: DIRECTIVE USED CONTAINS NO REQUIRED OPERANDS
    		currentline = (new Line(linecounter, "", "SWR","000", "000", LOCATION_COUNTER, "000", "000000000000"));
    		currentline.addError(010);
    		LOCATION_COUNTER++;
    	}
    }
    
    private static void parseAndCheckADCe (String[] tokens) {
    	
    	if (tokens.length > 2) {
    		String expression = tokens[2];
    		
    		// Check for leading star notation
    		if (expression.contains(",")) {
    			
    			// ERROR: EXPRESSION CANNOT CONTAIN COMMAS
				currentline = (new Line(linecounter, "", "SWR","000", "000", LOCATION_COUNTER, "000", "000000000000"));
				currentline.addError(404);
				LOCATION_COUNTER++;
    		}
    		else if (expression.contains("=")) {
    			// ERROR: EXPRESSIONS CANNOT HAVE LITERALS AS OPERANDS
				currentline = (new Line(linecounter, "", "SWR","000", "000", LOCATION_COUNTER, "000", "000000000000"));
				currentline.addError(617);
				LOCATION_COUNTER++;
    		}
    		else if (tokens[2].contains("(") || tokens[2].contains(")")) {
    			// ERROR: DIRECTIVES ARE NOT ALLOWED TO USE MEMORY REFERENCES WITH AN INDEX: LINE WILL BE REPLACED BY A NOP
				currentline = (new Line(linecounter, "", "SWR","000", "000", LOCATION_COUNTER, "000", "000000000000"));
				currentline.addError(616);
				LOCATION_COUNTER++;
    		}
    		// Check for operator as last character of expression
			else if (expression.charAt(expression.length()-1) == '+' || expression.charAt(expression.length()-1) == '-') {
				
				// ERROR: INVALID EXPRESSION, CANT HAVE LAST VALUE BE OPERATOR
				currentline = (new Line(linecounter, "", "SWR","000", "000", LOCATION_COUNTER, "000", "000000000000"));    							LOCATION_COUNTER++;
				currentline.addError(403);
			}
    		
    		// Check for back to back operators
			else if (expression.contains("++") || expression.contains("--") || expression.contains("+-") || expression.contains("-+")) {
				
				// ERROR: INVALID EXPRESSION, CANNOT HAVE CONSECUTIVE OPERATORS IN EXPRESSION
				currentline = (new Line(linecounter, "", "SWR","000", "000", LOCATION_COUNTER, "000", "000000000000"));    							LOCATION_COUNTER++;
				currentline.addError(402);
			}
    		else if (tokens[2].charAt(0) == '*') {
    			expression = tokens[2].substring(1);
    			if (expression.contains("*")) {
    				
    				// ERROR: ONLY ONE STAR IS ALLOWED IN AN ADC-EXPRESSION
    				currentline = (new Line(linecounter, "", "SWR","000", "000", LOCATION_COUNTER, "000", "000000000000"));
    				currentline.addError(279);
    				LOCATION_COUNTER++;
    			}
    			else if (expression.charAt(0) != '+' && expression.charAt(0) != '-') {
    				// ERROR: STAR NOTATION MUST BE FOLLOWED BY EITHER A + OR - OPERATOR
    				currentline = (new Line(linecounter, "", "SWR","000", "000", LOCATION_COUNTER, "000", "000000000000"));
    				currentline.addError(306);
    				LOCATION_COUNTER++;
    			}
    			else {
    				String[] operands = getExpressionOperands(expression);
    				if (operands.length > 5) {
    					// ERROR: EXPRESSION MAY HAVE 3 OPERATORS AT MOST
        				currentline = (new Line(linecounter, "", "SWR","000", "000", LOCATION_COUNTER, "000", "000000000000"));
        				currentline.addError(667);
        				LOCATION_COUNTER++;
    				}
    				else {
    					// Add valid line to intermediate
    					if (!tokens[0].equals("") && isValidLabel(tokens[0])) {
    						currentline = (new Line(linecounter, tokens[0], "ADCe",tokens[2], LOCATION_COUNTER, "000", "000000000000"));
    						updateSymbolTable(tokens);
    					}
    					else if (!tokens[0].equals("")) {
    						currentline = (new Line(linecounter, "", "ADCe",tokens[2], LOCATION_COUNTER, "000", "000000000000"));
    						checkForValidLabel(tokens[0]);
    					}
    					else {
    						currentline = (new Line(linecounter, "", "ADCe",tokens[2], LOCATION_COUNTER, "000", "000000000000"));
    					}
    					checkAdcExpressionOperandSyntax(operands);
    					LOCATION_COUNTER++;
    				}
    			}
    			
    		}
    		else if (tokens[2].contains("*")) {
    			// ERROR: IF THERE IS A STAR IN THE ADC-EXPRESSION, IT MUST BE APPEAR FIRST IN THE OPERAND
    			currentline = (new Line(linecounter, "", "SWR","000", "000", LOCATION_COUNTER, "000", "000000000000"));
    			currentline.addError(280);
    			LOCATION_COUNTER++;
    		}
    		else {
    			String[] operands = getExpressionOperands(tokens[2]);
				// Add valid line to intermediate
				if (!tokens[0].equals("") && isValidLabel(tokens[0])) {
					currentline = (new Line(linecounter, tokens[0], "ADCe",tokens[2], LOCATION_COUNTER, "000", "000000000000"));
					updateSymbolTable(tokens);
				}
				else if (!tokens[0].equals("")) {
					currentline = (new Line(linecounter, "", "ADCe",tokens[2], LOCATION_COUNTER, "000", "000000000000"));
					checkForValidLabel(tokens[0]);
				}
				else {
					currentline = (new Line(linecounter, "", "ADCe",tokens[2], LOCATION_COUNTER, "000", "000000000000"));
				}
				checkAdcExpressionOperandSyntax(operands);
				LOCATION_COUNTER++;
    		}
    		
    	}
    	else {
    		// ERROR: THIS CALL TO A DIRECTIVE HAS NO REQUIRED OPERANDS IN THE OPERAND FIELD
			currentline = (new Line(linecounter, "", "SWR","000", "000", LOCATION_COUNTER, "000", "000000000000"));
			currentline.addError(010);
			LOCATION_COUNTER++;
    	}
    }
    
    private static void parseAndCheckNOP (String[] tokens) {
    	Boolean invalid_label = false, invalid_operand = false;
    	if (!tokens[0].equals("")) {
    		//ERROR: UNABLE TO ASSIGN LABEL TO THIS DIRECTIVE"
    		invalid_label = true;      
    	}
        if (tokens.length > 2) {
    		//ERROR: THE NOP DIRECTIVE DOES NOT HAVE AN OPERAND FIELD"
    		invalid_operand = true;  
        }
		currentline = (new Line(linecounter, "", "SWR","000", "000", LOCATION_COUNTER, "000", "000000000000"));
		
		//Print error if label is present
		if (invalid_label) {
			currentline.addError(160);
		}
		if (invalid_operand) {
			currentline.addError(211);
		}
		LOCATION_COUNTER++;
        
    }

    private static boolean hasMultipleOperands (String token) {
        /*
         *  hasMultipleOperands checks the operand field string in tokens[2] and checks
         *  for either a "*" or "," and returns true if it does.
         */
        boolean result = false;
        if (token.contains("*") || token.contains(",")) {
            result = true;
        }
        return result;
    }
    
    private static String extendBinary (String binary, int digits) {
        while (binary.length() < digits) {
            binary = "0" + binary;
        }
        return binary;
    }
    
    private static String extendHex (String hex, int digits) {
        while (hex.length() < digits) {
            hex = "0" + hex;
        }
        return hex;
    }
    
    private static String asciiToHex (String characters) {
        String ascii_hex = new String();
        for (char c : characters.toCharArray()) {
        	if (c == ' ') {
        		ascii_hex += "20";
        	}
        	else {
        		ascii_hex += Integer.toHexString(c);        
        	}
        }
        while (ascii_hex.length() < 8) {
        	ascii_hex += "20";
        }
        return ascii_hex;
    }
    
    private static String binToHex(String binary) {
    	String result = "";
    	if(binary.length()==32){
    		for(int i=0; i<32; i+=4){
    			result += Integer.toHexString(Integer.parseInt(binary.substring(i, i+4), 2));
    		}
    	}else{
    		return result;
    	}
    	return result;
    }
    
    private static boolean isNumber (String string) {
    	if(string.matches("[0-9]+")){
    		return true;
    	}else{
    		return false;
    	}
    }
    
    private static void checkForValidLabel (String label) {
        boolean check = true, valid_characters = false;
        char[] characters = label.toCharArray();
        int i = 0;
        
            // Check leading character
            if (!(Character.isLowerCase(characters[0])) && !(Character.isUpperCase(characters[0]))) {
                //ERROR: LEADING CHARACTER OF LABEL MUST BE A-Z OR a-z"
                currentline.addError(220);    
            }
            // Check each character of label
            while (i < label.length() && check) {
                if (Character.isLowerCase(characters[i]) || Character.isUpperCase(characters[i]) || Character.isDigit(characters[i])) {    
                    valid_characters = true;
                }
                else {
                    check = false;
                    valid_characters = false;
                }
                i++;
            }
            if (!valid_characters) {
                //ERROR: LABEL MUST ONLY CONTAIN UPPER AND LOWER CASE LETTERS AND NUMBERS"
                currentline.addError(221);  
            }
            
            // Check length of label
            if (!(label.length() <= 32) || !(label.length() > 1)) {
                //ERROR: LABEL LENGTH NOT WITHIN LIMIT, LABEL MUST BE BETWEEN 1 AND 32 CHARACTERS"
                currentline.addError(222);    
            }  
    }
    
    private static boolean areValidEquExpressionOperands (String[] operands) {
    	int i = 0;
    	Boolean valid = true;
		// Check syntax for each operand
		while (i < operands.length && operands.length != 0) {
			//Check syntax for numerical operand
			if (isNumber(operands[i])) {
				if (!(Double.valueOf(operands[i]) >= 0) || !(Double.valueOf(operands[i]) <= 4095)) {
					valid = false;
				}
			}
			else if (operands[i].equals("*")) {
				valid = true;
			}
			// Check if operand is an external reference
			else if (SYMBOL_TABLE.symbolIsDefined(operands[i]) && SYMBOL_TABLE.getUsage(operands[i]).equals("EXT")) {
				valid = false;
			}
			else if (!SYMBOL_TABLE.symbolIsDefined(operands[i])) {
				valid = false;
			}
			else if (isLiteral(operands[i])) {
				valid= false;
			}
			i++;
		}
		return valid;
    }
    
    private static boolean areValidAdcExpressionOperands (String[] operands) {
    	int i = 0;
    	Boolean valid = true;
		// Check syntax for each operand
		while (i < operands.length && operands.length != 0) {
			//Check syntax for numerical operand
			if (isNumber(operands[i])) {
				if (!(Double.valueOf(operands[i]) >= 0) || !(Double.valueOf(operands[i]) <= 4095)) {
					valid = false;
				}
			}
			else if (operands[i].equals("*")) {
				valid = true;
			}
			// Check if operand is an external reference
			else if (SYMBOL_TABLE.symbolIsDefined(operands[i]) && SYMBOL_TABLE.getUsage(operands[i]).equals("EXT")) {
				valid = true;
			}
			else if (!SYMBOL_TABLE.symbolIsDefined(operands[i])) {
				valid = false;
			}
			else if (isLiteral(operands[i])) {
				valid= false;
			}
			i++;
		}
		return valid;
    }
    private static boolean isLiteral(String literal) {
    	boolean answer = false;
    	if (literal.charAt(0) == '=') {
    		answer = true;
    	}
    	return answer;
    }
    
    private static boolean isValidLabel (String label) {
    	boolean check = true, valid_characters = false, valid_length = true, valid_lead_ch = true, answer = false;
        char[] characters = label.toCharArray();
        int i = 0;
        
            // Check leading character
            if (!(Character.isLowerCase(characters[0])) && !(Character.isUpperCase(characters[0]))) {
                valid_lead_ch = false;
            }
            // Check each character of label
            while (i < label.length() && check) {
                if (Character.isLowerCase(characters[i]) || Character.isUpperCase(characters[i]) || Character.isDigit(characters[i])) {    
                    valid_characters = true;
                }
                else {
                    check = false;
                    valid_characters = false;
                }
                i++;
            }
            // Check length of label
            if (!(label.length() <= 32) || !(label.length() > 0)) {
                valid_length = false;
            }  
            
            // Set valid_symbol to true to update to table
            if (valid_lead_ch && valid_characters && valid_length) {
            	answer = true;
            }
            return answer;
    }
    
    private static boolean isValidMemoryReference (String memory_reference) {
    	Boolean valid = true;
    	if (memory_reference.charAt(0) == '=' && memory_reference.length() > 1) {
    		String literal_value = memory_reference.substring(1);
    		if (literal_value.contains("(")) {
    			valid = false;
    		}
    		else if (literal_value.charAt(0) == '+' || literal_value.charAt(0) == '-') {
    			literal_value = literal_value.substring(1);
    		}
    		
    		if (literal_value.length() > 0) {
    			if (literal_value.charAt(literal_value.length()-1) == 'B' || literal_value.charAt(literal_value.length()-1) == 'b') {
    				if (!isValidBinary(literal_value.substring(0, literal_value.length()-1))) {
    					valid = false;
    				}
    			}
    			else if (literal_value.charAt(literal_value.length()-1) == 'H' || literal_value.charAt(literal_value.length()-1) == 'h') {
    				if (!isValidHex(literal_value.substring(0,literal_value.length()-1))) {
    					valid = false;
    				}
    			}
    			else if (!isNumber(literal_value)) {
    				valid = false;
    			}
    		}
    		else {
    			valid = false;
    		}
    	}
    	else if (memory_reference.charAt(0) == '=') {
    		valid = false;
    	}
    	else if (memory_reference.contains("!") || memory_reference.contains("?") || memory_reference.contains("#") || 
				memory_reference.contains("_") || memory_reference.contains("/") || memory_reference.contains("\"") || 
				memory_reference.contains(".") || memory_reference.contains("@") || memory_reference.contains("$") ||
				memory_reference.contains("%") || memory_reference.contains("^") || memory_reference.contains("&") ||
				memory_reference.contains("[") || memory_reference.contains("]") || memory_reference.contains("{") ||
				memory_reference.contains("}") || memory_reference.contains("<") || memory_reference.contains(">")) {
    		// ERROR: INVALID CHARACTERS
			valid = false; 

		}
    	// Check star notation syntax
    	else if (memory_reference.charAt(0) == '*') {
    		if (memory_reference.length() > 1) {
    			if (memory_reference.charAt(1) == '+' || memory_reference.charAt(1) == '-') {
    				if (memory_reference.length() > 2) {
    					Boolean negative = false;
    					if (memory_reference.charAt(1) == '-') {
    						negative = true;
    					}
    					memory_reference = memory_reference.substring(2);

    					if (memory_reference.contains("*")) {
    						valid = false;
    					}
    					else if (isNumber(memory_reference)) {
    						int result = 0;
    						if (negative) {
    							result = LOCATION_COUNTER - Integer.valueOf(memory_reference);
    						}
    						else {
    							result = LOCATION_COUNTER + Integer.valueOf(memory_reference);
    						}
    						if (!(result >= 0) || !(result <= 4095)) {
    							valid = false;
    						}
    					}
    					else if (!SYMBOL_TABLE.symbolIsDefined(memory_reference)){
    						// ERROR: NUMERICAL VALUE MUST FOLLOW A + OR - IN STAR NOTATION
    						valid = false;
    					}
    				}
    				else {
    					valid = false;
    				}
    			}
    			else {
    				valid = false;    			}
    		}
    	}
    	// Check for star not at first index
    	else if (memory_reference.contains("*") && memory_reference.charAt(0) != '*') {
    		valid = false;
    	}
    	else if (memory_reference.contains("++") || memory_reference.contains("--") || 
    			memory_reference.contains("+-") || memory_reference.contains("-+")) {
    		valid = false;
    	}
    	else if (memory_reference.charAt(0) == '+' || memory_reference.charAt(0) == '-') {
    		valid = false;
    	}
    	else if (memory_reference.charAt(memory_reference.length()-1) == '+' || memory_reference.charAt(memory_reference.length()-1) == '-') {
    		valid = false;
    	}
    	// Check number bounds
    	else if (isNumber(memory_reference)) {
    		if (!(Double.valueOf(memory_reference) >= 0) || !(Double.valueOf(memory_reference) <= 4095)) {
    			valid = false;			}
    	}
    	// Check indexed memory reference syntax
    	else if (memory_reference.contains("(")){
    		int i = 0, j = 0;
    		 
    		// Check for closing parentheses
    		if (!memory_reference.contains(")")) {
    			valid = false;
    		}
    	
    		while (memory_reference.charAt(i) != '(') {
    			i++;
    		}
    		String first_operand = memory_reference.substring(0, i);
    	
    		// Check for index value between parentheses
    		if (memory_reference.substring(i).length() < 3) {
    			valid = false;
    		}
    		// Check if first operand is in range
    		if (isNumber(first_operand)) {
    			if (!(Double.valueOf(first_operand) >= 0) || !(Double.valueOf(first_operand) <= 4095)) {
    				valid = false;    			
    			}
    		}
    		// Check if symbol is defined and not external
    		else if (SYMBOL_TABLE.symbolIsDefined(first_operand)) {
    			valid = false;
    		}
    		else if (isLiteral(first_operand)) {
    			valid = false;
    		}

    		j = i+1;
    		if (valid) {
    			while (memory_reference.charAt(j) != ')') {
    				j++;
    			}
    			String second_operand = memory_reference.substring(i+1, j);

    			// Check if index value is number
    			if (isNumber(second_operand)) {
    				if (!(Double.valueOf(second_operand) >= 1) || !(Double.valueOf(second_operand) <= 7)) {
    					valid = false;
    				}
    			}
    			else if (isLiteral(second_operand)) {
    				valid = false;
    			}
    			else if (second_operand.contains("*")) {
    				valid = false;
    			}
    			else if (!SYMBOL_TABLE.symbolIsDefined(second_operand)) {
    				valid = false;
    			}
    			else if (second_operand.contains("+") || second_operand.contains("-")) {
    				valid = false;
    			}
    			else if (SYMBOL_TABLE.symbolIsDefined(second_operand) && SYMBOL_TABLE.getUsage(second_operand).equals("EXT")) {
    				valid = false;
    			}
    			else if (SYMBOL_TABLE.symbolIsDefined(second_operand)) {
    				int value = Integer.valueOf(SYMBOL_TABLE.getString(second_operand));
    				if (value < 1 || value > 7) {
    					valid = false;
    				}
    			}
    		}
    	}
    	else {
    		if (!isValidLabel(memory_reference)) {
    			valid = false;
    		}
    	}
    	return valid;
    }
    
    private static void checkEquExpressionOperandSyntax (String[] operands) {
    	int i = 0;
		// Check syntax for each operand
		while (i < operands.length && operands.length != 0) {
			//Check syntax for numerical operand
			if (isNumber(operands[i])) {
				if (!(Double.valueOf(operands[i]) >= 0) || !(Double.valueOf(operands[i]) <= 4095)) {
					// ERROR: MEMORY REFERENCE OUT OF RANGE
					currentline.addError(115);
				}
			}
			
			// Check if operand is an external reference
			else if (SYMBOL_TABLE.symbolIsDefined(operands[i]) && SYMBOL_TABLE.getUsage(operands[i]).equals("EXT")) {
				// ERROR: OPERANDS IN AN EQU-EXPRESSION CANNOT BE AN EXTERNAL REFERENCE
				currentline.addError(296);
			}
			else if (!SYMBOL_TABLE.symbolIsDefined(operands[i])) {
				// ERROR: SYMBOL MUST BE PREVIOUSLY EQUATED BEFORE BEING USED IN AN EQU-EXPRESSION
				currentline.addError(297);
				checkForValidLabel(operands[i]);
			}
			else if (isLiteral(operands[i])) {
				// ERROR: LITERALS ARE AN INVALID OPERAND FOR AN EQU-EXPRESSION
				currentline.addError(298);
			}
			i++;
		}
    }
    
    private static void checkAdcExpressionOperandSyntax (String[] operands) {
    	int i = 0;
		// Check syntax for each operand
		while (i < operands.length && operands.length != 0) {
			//Check syntax for numerical operand
			if (isNumber(operands[i])) {
				if (!(Double.valueOf(operands[i]) >= 0) || !(Double.valueOf(operands[i]) <= 4095)) {
					// ERROR: MEMORY REFERENCE OUT OF RANGE
					currentline.addError(115);
				}
			}
			else if (isLiteral(operands[i])) {
				// ERROR: LITERALS ARE AN INVALID OPERAND FOR AN EQU-EXPRESSION
				currentline.addError(298);
			}
			else if (operands[i].contains("!") || operands[i].contains("?") || operands[i].contains("#") || 
					operands[i].contains("_") || operands[i].contains("/") || operands[i].contains("\"") || 
					operands[i].contains(".") || operands[i].contains("@") || operands[i].contains("$") ||
					operands[i].contains("%") || operands[i].contains("^") || operands[i].contains("&") ||
					operands[i].contains("[") || operands[i].contains("]") || operands[i].contains("{") ||
					operands[i].contains("}") || operands[i].contains("<") || operands[i].contains(">")) {
				// ERROR: ADDRESS CONSTANT CONTAINS INVALID CHARACTERS 
    			currentline.addError(621); 

			}
			// Check if operand is an external reference
			else if (SYMBOL_TABLE.symbolIsDefined(operands[i]) && SYMBOL_TABLE.getUsage(operands[i]).equals("EXT")) {
				// ERROR: OPERANDS IN AN EQU-EXPRESSION CANNOT BE AN EXTERNAL REFERENCE
				currentline.addError(296);
			}
			else if (!SYMBOL_TABLE.symbolIsDefined(operands[i])) {
				// ERROR: SYMBOL ISNT EQUATED, WILL BE CHECKED IN PASS 2
				currentline.addError(310);
				checkForValidLabel(operands[i]);
			}
			
			i++;
		}
    }
    private static void checkMemoryReferenceSyntax (String memory_reference) {
    	if (memory_reference.charAt(0) == '=' && memory_reference.length() > 1) {
    		memory_reference = memory_reference.substring(1);
    		if (memory_reference.charAt(0) == '+' || memory_reference.charAt(0) == '-') {
    			memory_reference = memory_reference.substring(1);
    		}
    		if (memory_reference.length() > 0) {
    			if (memory_reference.charAt(memory_reference.length()-1) == 'B' || memory_reference.charAt(memory_reference.length()-1) == 'b') {
    				if (!isValidBinary(memory_reference.substring(0,memory_reference.length()-1))) {
    					// ERROR: INVALID BINARY LITERAL FORMAT
    					currentline.addError(273);
    				}
    			}
    			else if (memory_reference.charAt(memory_reference.length()-1) == 'H' || memory_reference.charAt(memory_reference.length()-1) == 'h') {
    				if (!isValidHex(memory_reference.substring(0,memory_reference.length()-1))) {
    					// ERROR: INVALID Hex LITERAL FORMAT
    					currentline.addError(274);
    				}
    			}
    			else if (!isNumber(memory_reference)) {
    				// ERROR: INVALID LITERAL SYNTAX
    				currentline.addError(272);
    			}
    		}
    		else {
    			// ERROR: INVALID LITERAL SYNTAX
        		currentline.addError(272);
    		}
    	}
    	else if (memory_reference.charAt(0) == '=') {
    		// ERROR: INVALID LITERAL SYNTAX
    		currentline.addError(272);
    	}
    	else if (memory_reference.contains("!") || memory_reference.contains("?") || memory_reference.contains("#") || 
				memory_reference.contains("_") || memory_reference.contains("/") || memory_reference.contains("\"") || 
				memory_reference.contains(".") || memory_reference.contains("@") || memory_reference.contains("$") ||
				memory_reference.contains("%") || memory_reference.contains("^") || memory_reference.contains("&") ||
				memory_reference.contains("[") || memory_reference.contains("]") || memory_reference.contains("{") ||
				memory_reference.contains("}") || memory_reference.contains("<") || memory_reference.contains(">")) {
    		// ERROR: INVALID CHARACTERS
			currentline.addError(621); 

    	}
    	else if (memory_reference.charAt(0) == '*') {
    		if (memory_reference.length() > 1) {
    			if (memory_reference.charAt(1) == '+' || memory_reference.charAt(1) == '-') {
    				if (memory_reference.length() > 2) {
    					Boolean negative = false;
    					if (memory_reference.charAt(1) == '-') {
    						negative = true;
    					}
    					memory_reference = memory_reference.substring(2);
    					if (memory_reference.contains("*")) {
    						// ERROR: ONLY ONE STAR IS ALLOWED IN AN INSTRUCTION OPERAND
    						currentline.addError(289);
    					}
    					else if (isNumber(memory_reference)) {
    						int result = 0;
    						if (negative) {
    							result = LOCATION_COUNTER - Integer.valueOf(memory_reference);
    						}
    						else {
    							result = LOCATION_COUNTER + Integer.valueOf(memory_reference);
    						}
    						if (!(result >= 0) || !(result <= 4095)) {
    							currentline.addError(115);
    						}
    					}
    					else if (!SYMBOL_TABLE.symbolIsDefined(memory_reference)){
    						// ERROR: NUMERICAL VALUE MUST FOLLOW A + OR - IN STAR NOTATION
    						currentline.addError(264);
    					}
    				}
    				else {
    					// ERROR: VALUE MUST FOLLOW _0
    					currentline.addError(264);
    				}
    			}
    			else {
    				// ERROR: INVALID SYNTAX FOR STAR NOTATION, MUST BE *, *+number, OR *-number
    				currentline.addError(308);

    			}
    		}
    	}
    	else if (memory_reference.contains("++") || memory_reference.contains("--") || 
    			memory_reference.contains("+-") || memory_reference.contains("-+")) {
    		// ERROR: BACK TO BACK OPERATORS
    		currentline.addError(311);

    	}
    	else if (memory_reference.charAt(0) == '+' || memory_reference.charAt(0) == '-' ||
    			memory_reference.charAt(memory_reference.length()-1) == '+' || memory_reference.charAt(memory_reference.length()-1) == '-') {
    		// ERROR: MEMORY REFERENCES CANNOT START OR END WITH AN OPERATOR
    		currentline.addError(637);

    	}

    	else if (isNumber(memory_reference)) {
    		if (!(Double.valueOf(memory_reference) >= 0) || !(Double.valueOf(memory_reference) <= 4095)) {
    			currentline.addError(115);
    		}
    	}
    	else if (memory_reference.contains("(")){
    		if (memory_reference.contains(")")) {
    			int i = 0, j = 0;
    			while (memory_reference.charAt(i) != '(') {
    				i++;
    			}
    			if (memory_reference.substring(i).length() > 2) {
    				if (!memory_reference.contains("*")) {
    					String first_operand = memory_reference.substring(0, i);
    					if (isNumber(first_operand)) {
    						if (!(Double.valueOf(first_operand) >= 0) || !(Double.valueOf(first_operand) <= 4095)) {
    							currentline.addError(115);
    						}
    					}
    					else if (isLiteral(first_operand)) {
    						// ERROR: LITERAL CANT BE USED IN INDEX MEMORY REFERENCES
    						currentline.addError(636);

    					}
    					else {
    						checkForValidLabel(first_operand);
    					}
    					j = i+1;

    					while (memory_reference.charAt(j) != ')') {
    						j++;
    					}

    					String second_operand = memory_reference.substring(i+1, j);
    					if (isNumber(second_operand)){
    						if (!(Double.valueOf(second_operand) >= 1) || !(Double.valueOf(second_operand) <= 7)) {
    							// ERROR: INVALID INDEX REGISTER USED
    							currentline.addError(265);
    						}
    					}
    					else if (isLiteral(second_operand)) {
    						// ERROR: LITERALS CANNOT BE USED IN A MEMORY INDEX FIELD
    						currentline.addError(633);
    					}
    					else if (second_operand.contains("*")) {
    						// ERROR: STAR NOTATION CANNOT BE USED IN A MEMORY INDEX FIELD
    						currentline.addError(634);

    					}
    					else if (second_operand.contains("+") || second_operand.contains("-")) {
    						// ERROR: EXPRESSIONS CANNOT BE USED IN A MEMORY INDEX FIELD
    						currentline.addError(635);

    					}
    					else if (!SYMBOL_TABLE.symbolIsDefined(second_operand)) {
    						// ERROR: SYMBOL IN MEMORY INDEX FIELD MUST BE PREVIOUSLY EQUATED
    						currentline.addError(632);
    					}
    					else if (SYMBOL_TABLE.symbolIsDefined(second_operand)) {
    						int result = Integer.valueOf(SYMBOL_TABLE.getString(second_operand));
    						if (result < 1 || result > 7) {
    							// ERROR: 
    							currentline.addError(265);
    						}
    					}
    				}
    				else {
    					// ERROR: STAR NOTATION CANNOT BE USED IN INDEX FIELD
    					currentline.addError(634);
    				}
    			}
    			else {
    				// ERROR: INDEX HAS TO HAVE VALUE IN PARENS
        			currentline.addError(506);

    			}
    		}
    		else {
    			// ERROR: INDEXED MEMORY REFERENCE MUST HAVE A CLOSING PARENTHESES
    			currentline.addError(611);
    		}
    	}
    	else if (memory_reference.contains(")")) {
    		// ERROR: INDEXED MEMORY REFERENCE MUST HAVE AN OPENING PARENTHESES
    		currentline.addError(507);
    	}
    	else if (memory_reference.contains("*") && memory_reference.charAt(0) != '*') {
    		// ERROR: STAR NOTATION MUST BE THE FIRST CHARACTER OF AN INSTRUCTION OPERAND
    		currentline.addError(290);
    	}
    	else {
    		// WARNING: SYMBOL USED FOR MEMORY REFERENCE HAS YET TO BE DEFINED: CHECKED IN PASS 2
    		currentline.addError(310);
    		checkForValidLabel(memory_reference);
    	}
    }

    private static void checkGeneralLineSyntax (String[] tokens) {
    	Boolean label_only = false;
    	
    	if (linecounter == 1 && !tokens[1].equals("STT")) {
    		// ERROR: STT MUST BE THE FIRST LINE OF THE PROGRAM
    		outputPrint("**FATAL** FIRST LINE OF PROGRAM MUST USE STT DIRECTIVE: ASSEMBLY WILL BE STOPPED");
    		currentline.addError(001);
    		valid_start = false;
    		continue_assembly = false;
    	}
    	// Check for too many commas
    	if (!tokens[1].equals("CLRA") && !tokens[1].equals("CLRX") && tokens.length > 2) {
    		if (tokens[2].contains(",,")) {
    			currentline = (new Line(linecounter, "", "SWR","000", "000", LOCATION_COUNTER, "000", "000000000000"));
    			skip = true;
    			currentline.addError(606);
    			LOCATION_COUNTER++;
    		}
    	}
    	// Check for label and no instruction/operands
    	if ((tokens.length < 2 || tokens[1].charAt(0) == ':') && !tokens[0].isEmpty()) {
    		currentline.addError(618);
    		label_only = true;
    		skip = true;
    	}
    	// Check for too many symbols
    	if (SYMBOL_TABLE.size() > 50) {
    		continue_assembly = false;
    		skip = true;
    		outputPrint("**FATAL** EXCEEDED THE ALLOWED NUMBER OF SYMBOLS: ASSEMBLY WILL BE STOPPED");
    		currentline.addError(601);
    	}
 
    	// Check if full line doesn't start in column 1
    	if (!label_only && tokens[1].charAt(0) == ':') {
    		skip = true;
    		currentline.addError(602);
    	}
    	// Check if LC is now out of bounds
    	if (LOCATION_COUNTER > 4095) {
    		continue_assembly = false;
    		skip = true;
    		outputPrint("**FATAL** LOCATION COUNTER IS NOW GREATER THAN 4095: ASSEMBLY WILL BE STOPPED");
    		currentline.addError(603);
    	}
    	else if (LOCATION_COUNTER < 0) {
    		continue_assembly = false;
    		skip = true;
    		outputPrint("**FATAL** LOCATION COUNTER IS NOW NEGATIVE: ASSEMBLY WILL BE STOPPED");
    		currentline.addError(604);
    	}
    }
    
    private static int getLiteralValue (String literal) {
    	int value = 0;
    	Boolean negative = false;
    	literal = literal.substring(1);
    	
    	if (literal.charAt(0) == '-' || literal.charAt(0) == '+') {
    		if (literal.charAt(0) == '-') {
    			negative = true;
    		}
    		literal = literal.substring(1);

    	}
    	if (literal.charAt(literal.length()-1) == 'B' || literal.charAt(literal.length()-1) == 'B') {
    		literal = literal.substring(0, literal.length()-1);
    		if (isValidBinary(literal)) {
    			if (negative) {
    				value = Integer.parseInt(literal, 2)*-1;
    			}
    			else {
        			value = Integer.parseInt(literal, 2);

    			}
    		}
    	}
    	else if (literal.charAt(literal.length()-1) == 'H' || literal.charAt(literal.length()-1) == 'h') {
    		literal = literal.substring(0, literal.length()-1);
    		if (isValidHex(literal)) {
    			if (negative) {
    				value = Integer.parseInt(literal, 16)*-1;
    			}
    			else {
        			value = Integer.parseInt(literal, 16);

    			}
    		}
    	}
    	else if (isNumber(literal)) {
    		if (negative) {
    			value = Integer.valueOf(literal)*-1;
    		}
    		else {
    			value = Integer.valueOf(literal);
    		}
    	}
    	return value;
    }
    
    private static String getSHex (String memory_reference) {
    	int i = 0;
		String s_field_hex = null;
		while (memory_reference.charAt(i) != '(') {
			i++;
		}
		String s_field = memory_reference.substring(0, i);
		if (isNumber(s_field)) {
			int s_field_int = Integer.valueOf(s_field);
			s_field_hex = Integer.toHexString(s_field_int);
		}
		else if (SYMBOL_TABLE.symbolIsDefined(s_field)) {
			s_field_hex = Integer.toHexString(SYMBOL_TABLE.getLocation(s_field));
		}
		else {
			s_field_hex = "???";
		}
		return s_field_hex;		
    }
    
    private static String getXBinary (String memory_reference) {
    	int i = 0;
		String x_field_binary = "0";
		while (memory_reference.charAt(i) != '(') {
			i++;
		}
		String x_field = memory_reference.substring(i+1, memory_reference.length()-1);
		if (isNumber(x_field)) {
			int x_field_int = Integer.valueOf(x_field);
			x_field_binary = Integer.toBinaryString(x_field_int);
			x_field_binary = extendBinary(x_field_binary, 3);
		}
		else if (SYMBOL_TABLE.symbolIsDefined(x_field)) {
			if(SYMBOL_TABLE.getString(x_field).contains("+")||SYMBOL_TABLE.getString(x_field).contains("-")){
				int x_field_int = evaluateEQUExpression(SYMBOL_TABLE.getString(x_field));
				x_field_binary = extendBinary(Integer.toString(x_field_int), 3);
			}else{
				x_field_binary = Integer.toBinaryString(Integer.valueOf(SYMBOL_TABLE.getString(x_field)));
				x_field_binary = extendBinary(x_field_binary, 3);
			}		
		}
		return x_field_binary;
    }
    private static int evaluateADCExpression(Line line) {
    	String exp = line.getOperands()[0];
    	String LC = Integer.toString(line.getLC());
    	String temp = exp;
        String delims = "[\\+\\-]";
        ArrayList<Integer> signarray = new ArrayList<Integer>();
        int total = 0;
        
        if(exp.charAt(0)=='+'||exp.charAt(0)=='-'){
            temp = "0".concat(temp);
            signarray.add(-1);
        }else{
            temp = "0+".concat(temp);
            signarray.add(1);
        }
        
        for(int i=0; i<exp.length(); i++){
        	if(exp.charAt(i)=='+'){
        		signarray.add(1);
        	}else if(exp.charAt(i)=='-'){
        		signarray.add(-1);
        	}
        }
        
        String[] operands = temp.split(delims);
       
        for(int x = 1; x < operands.length; x++){
        	total += (evaluateADCe(operands[x], LC)*signarray.get(x-1));  
        }    
        return total;
    }
    
    private static int evaluateEQUExpression(String exp){
        String temp = exp;
        String delims = "[\\+\\-]";
        ArrayList<Integer> signarray = new ArrayList<Integer>();
        int total = 0;
        
        if(exp.charAt(0)=='+'||exp.charAt(0)=='-'){
            temp = "0".concat(temp);
            signarray.add(-1);
        }else{
            temp = "0+".concat(temp);
            signarray.add(1);
        }
        
        for(int i=0; i<exp.length(); i++){
        	if(exp.charAt(i)=='+'){
        		signarray.add(1);
        	}else if(exp.charAt(i)=='-'){
        		signarray.add(-1);
        	}
        }
        
        String[] operands = temp.split(delims);
       
        for(int x = 1; x < operands.length; x++){
        	total += (evaluate(operands[x])*signarray.get(x-1));  
        }    
        return total;
    }
    
    private static int evaluateADCe (String operand, String LC) {

    	if(isNumber(operand)){
    		return Integer.valueOf(operand);   
    	}
    	else if(SYMBOL_TABLE.symbolIsDefined(operand) && !SYMBOL_TABLE.getUsage(operand).equals("EXT")){
    		return evaluateADCe(Integer.toString(SYMBOL_TABLE.getLocation(operand)), LC);
    	}
    	else if (SYMBOL_TABLE.symbolIsDefined(operand) && SYMBOL_TABLE.getUsage(operand).equals("EXT")) {
    		return evaluateADCe("0", LC);
    	}
    	else if (operand.equals("*")) {
    		return evaluateADCe(LC, LC);
    	}
    	else{
    		return 0;
    	}
    }
    
    private static int evaluate(String operand) {
        if(isNumber(operand)){
            return Integer.valueOf(operand);   
        }
        else if(SYMBOL_TABLE.symbolIsDefined(operand)){
            return evaluate(SYMBOL_TABLE.getString(operand));
        }
        else if (operand.equals("*")) {
        	return evaluate(Integer.toString(LOCATION_COUNTER));
        }
        else{
            return 0;
        }
    }
    private static Boolean isValidLiteral (String literal) {
    	Boolean valid = true;
    	String literal_value = literal.substring(1);
		if (literal_value.contains("(")) {
			valid = false;
		}
		else {
			if (literal_value.charAt(0) == '+' || literal_value.charAt(0) == '-') {
				literal_value = literal_value.substring(1);
			}
			
			if (literal_value.contains("+") || literal_value.contains("-")) {
				valid = false;
			}
			if (literal_value.charAt(literal_value.length()-1) == 'B' || literal_value.charAt(literal_value.length()-1) == 'b') {
				if (!isValidBinary(literal_value.substring(0, literal_value.length()-1))) {
					valid = false;
				}
			}
			else if (literal_value.charAt(literal_value.length()-1) == 'H' || literal_value.charAt(literal_value.length()-1) == 'h') {
				if (!isValidHex(literal_value.substring(0,literal_value.length()-1))) {
					valid = false;
				}
			}
			else if (!isNumber(literal_value)) {
				valid = false;
			}
		}
		return valid;
    }
    
    private static Boolean isValidBinary (String binary) {
    	if(binary.matches("[0-1]+")){
    		return true;
    	}else{
    		return false;
    	}
    }

    private static Boolean isValidHex (String hex) {
    	if (hex.matches("[a-fA-F0-9]+")) {
    		return true;
    	}
    	else {
    		return false;
    	}
    }
    private static String[] getExpressionOperands (String expression){
    	String temp = expression;
        String delims = "[\\+\\-]";
        
        if (expression.charAt(0)=='+'||expression.charAt(0)=='-') {
            temp = "0".concat(temp);
        } 
        else {
            temp = "0+".concat(temp);
        }
        String[] operands = temp.split(delims);
        return operands;
    }
    
    private static void poolLiteralsAtEnd () {
    	PRE_LITERAL_POOL_LC = LOCATION_COUNTER;
    	for (int i=0; i<SYMBOL_TABLE.size(); i++) {
    		if (SYMBOL_TABLE.get(i).GetUsage().equals("LITERAL")) {
    			int value = Integer.valueOf(getLiteralValue(SYMBOL_TABLE.get(i).GetLabel()));
    			
				currentline = (new Line(linecounter, SYMBOL_TABLE.get(i).GetLabel(), "NUM",Integer.toString(value), LOCATION_COUNTER, "000", "000000000000"));

				SYMBOL_TABLE.updateLocation(SYMBOL_TABLE.get(i).GetLabel(), LOCATION_COUNTER);
    			LOCATION_COUNTER++;
    			
    		}
    	}
    }
    
    private static String[] getExpressionSigns (String expression){

        ArrayList<String> signarray = new ArrayList<String>();
        
        signarray.add("+");
        if(expression.charAt(0)=='-'){
            signarray.add("-");
        }else{
            signarray.add("+");
        }
        
        for(int i=0; i<expression.length(); i++){
        	if(expression.charAt(i)=='+'){
                signarray.add("+");
        	}else if(expression.charAt(i)=='-'){
                signarray.add("-");
        	}
        }
        
        String[] result = new String[10];
        
        for(int i=0; i<signarray.size(); i++){
        	result[i] = signarray.get(i);
        }
        return result;
    }
  
    private static String getInstructionLineHexCode (Line line) {
    	String hex_code = "";
    	if (INSTRUCTION_TABLE.isInstruction(line.getInstruction())) {
    		int format = INSTRUCTION_TABLE.findFormat(line.getInstruction());
    		
    		switch (format) {
    		
    		case 1: hex_code = getFormatOneHexCode(line); break;
    		case 2: hex_code = getFormatTwoHexCode(line); break;
    		case 3: hex_code = getFormatThreeHexCode(line); break;
    		case 5: hex_code = getFormatFiveHexCode(line); break;
    		case 6: hex_code = getFormatSixHexCode(line); break;
    		case 7: hex_code = getFormatSevenHexCode(line); break;
    		}
    	}
    	else if (INSTRUCTION_TABLE.isDirective(line.getInstruction())) {
    		String dir = line.getInstruction();
    		if (dir.equals("CHC")) {
    			hex_code = line.getOperands()[0];
    		}
    		else if (dir.equals("NUM")) {
    			hex_code = extendHex(Integer.toHexString(Integer.valueOf(line.getOperands()[0])).toUpperCase(), 8);
    		}
    		else if (dir.equals("ADC")) {
    			hex_code = extendHex(line.getOperands()[0], 8);
    		}
    		else if (dir.equals("ADCE")) {
    			int result = evaluateADCExpression(line);
    			hex_code = extendHex(Integer.toHexString(result), 8);
    		}
    	}
    	
    	return hex_code;
    }

	private static String getFormatOneHexCode(Line line) {
		String[] operands = line.getOperands();		
		String instruction_binary = INSTRUCTION_TABLE.findOpcode(line.getInstruction());// Gets binary opcode
		instruction_binary += operands[0]; 												// Binary register
		instruction_binary += line.getXBin();											// Binary x field
		instruction_binary += "00";														// Unused bits
		instruction_binary += extendBinary(Integer.toBinaryString(START_PAGE),2);		// Binary page number
		instruction_binary += "0000";													// Unused bits
		instruction_binary += line.getSFieldBin();										// Binary s field
		
		return binToHex(instruction_binary).toUpperCase();   			// Convert integer to hex
	}
	
	private static String getFormatTwoHexCode(Line line) {
		String[] operands = line.getOperands();		
		String instruction_binary = INSTRUCTION_TABLE.findOpcode(line.getInstruction());// Gets binary opcode
		instruction_binary += operands[0]; 												// Binary register
		instruction_binary += "00000000000";											// Unused bits
		instruction_binary += line.getSFieldBin();										// Binary s field
		
		return binToHex(instruction_binary).toUpperCase();   			// Convert integer to hex
	}
	
	private static String getFormatThreeHexCode(Line line) {	
		String instruction_binary = INSTRUCTION_TABLE.findOpcode(line.getInstruction());// Gets binary opcode
		instruction_binary += "00000000000000";											// Unused bits
		instruction_binary += line.getSFieldBin();										// Binary s field
		
		return binToHex(instruction_binary).toUpperCase();   			// Convert integer to hex
	}
	
	private static String getFormatFiveHexCode(Line line) {
		String instruction_binary = INSTRUCTION_TABLE.findOpcode(line.getInstruction());// Gets binary opcode
		instruction_binary += line.getOperands()[0];									// dev
		instruction_binary += line.getXBin();											// x 
		instruction_binary += line.getOperands()[2];									// type
		instruction_binary += extendBinary(Integer.toBinaryString(START_PAGE),2); 		// Binary page number
		instruction_binary += line.getOperands()[3];									// Length
		instruction_binary += line.getSFieldBin();										// s
		
		return binToHex(instruction_binary).toUpperCase();   			// Convert integer to hex
	}
	
	private static String getFormatSixHexCode(Line line) {
		String instruction_binary = INSTRUCTION_TABLE.findOpcode(line.getInstruction());// Gets binary opcode
		instruction_binary += "000000";													// Unused bits
		instruction_binary += line.getOperands()[1]; 									// pg2
		instruction_binary += line.getOperands()[0]; 									// pg1
		instruction_binary += "0000000000000000";										// Unused bits
		
		return binToHex(instruction_binary).toUpperCase();   			// Convert integer to hex
	}
	
	private static String getFormatSevenHexCode(Line line) {	
		String instruction_binary = INSTRUCTION_TABLE.findOpcode(line.getInstruction());// Gets binary opcode
		instruction_binary += line.getOperands()[0]; 									// r1
		instruction_binary += line.getOperands()[1]; 									// r2
		instruction_binary += "00000000000000000000";									// Unused bits
		
		return binToHex(instruction_binary).toUpperCase();   			// Convert integer to hex
	}



}